0% found this document useful (0 votes)
11 views

EMB code

The document contains multiple code snippets for microcontroller programming using the 8051 architecture. It includes examples of UART communication, timer interrupts, and external interrupts, demonstrating how to send and receive data, control output ports, and handle interrupts. Each section is labeled with an experiment identifier (EXP) and showcases different functionalities of the microcontroller.

Uploaded by

dharshinip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

EMB code

The document contains multiple code snippets for microcontroller programming using the 8051 architecture. It includes examples of UART communication, timer interrupts, and external interrupts, demonstrating how to send and receive data, control output ports, and handle interrupts. Each section is labeled with an experiment identifier (EXP) and showcases different functionalities of the microcontroller.

Uploaded by

dharshinip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

EXP 3A

#include<Reg51.h>
int i=0;
sbit P3_5=P3^5;
sbit P2_1=P2^1;
unsigned char
a[10]={0x77,0x14,0xB4,0XB6,0XD4,0XE6,0XE7,0X34,0XF
7,0XF6};
void delay(int i){
unsigned int l,k;
for(k=0;k<i;k++){
for(l=0;l<1000;l++);
}
}
void send(int i){
P0=a[i];
P3_5=1;
P3_5=0;
}
void external0(void)interrupt 0{
send((i%10));
i++;
delay(1);
}
void main(){

P0=0;
P2=0;
P2_1=1;
P3=0;
P3_5=1;
P3_5=0;
IE=0X81;
IT0=1;
while(1){

}
}
EXP 3B
#include<Reg51.h>
sbit P3_5=P3^5;
sbit P2_1=P2^1;
sbit P1_0=P1^0;
int i=0;
unsigned char
a[10]={0x77,0x14,0xB4,0XB6,0XD4,0XE6,0XE7,0X34,0XF
7,0XF6};

void delay(int i){


unsigned int l,k;
for(k=0;k<i;k++){
for(l=0;l<1000;l++);
}
}
void send(int i){
P0=a[i];
P3_5=1;
P3_5=0;
}
void nINT0_ISR(void)interrupt 0{
send((i%10));
i++;
delay(1);
}
void main(){

P0=0;
P1=0xFF;
P2=0;
P2_1=1;
P3=0;
P3_5=1;
P3_5=0;
while(1);
}
EXP 4B
#include<Reg51.h>
sbit P3_1=P3^1;
sbit P3_7=P3^7;
sbit P3_5=P3^5;
unsigned char
a[10]={0X77,0X14,0XB3,0XB6,0XD4,0XE6,0XE7,0X34,0X
F7,0XF6};
int lc=0,i=0;
void send(){
P0=0;
P3_1=1;
P3_1=0;
}
void T0_Handler(void)interrupt 1{
TH0=0XFE;
TL0=0XFE;
lc++;
if(lc==5){
TR0=0;
lc=0;
P0=a[i%10];
i++;
P3_7=1;
P3_7=0;
P3_5=1;
P3_5=0;
TR0=1;
}
}
void main(){
TMOD=0X01;
TH0=0XFE;
TL0=0XFE;
TCON |=0X01;
ET0=1;
EA=1;
TR0=1;
P3_5=1;
P3_5=0;
P3_7=1;
P3_7=0;
while(1){
send();
}
}
EXP 4C
#include <Reg51.h>

sbit P3_1 = P3^1;


sbit P3_7 = P3^7;
sbit P3_5 = P3^5;

unsigned char a[10] = {0x77, 0x14, 0xB3, 0xB6, 0xD4,


0xE6, 0xE7, 0x34, 0xF7, 0xF6};
int lc = 0, i = 0;
void send() {
P0 = 0;
P3_1 = 1;
P3_1 = 0;
}
void delay(int i){
unsigned int l,k;
for(k=0;k<i;k++){
for(l=0;l<1000;l++);
}
}
void T0_Handler(void) interrupt 1 {
TH0 = 0xFE;
TL0 = 0xFE;

lc++;
if (lc == 5) {
TR0 = 0;
lc = 0;
delay(100);
P0 = a[i % 10];
i++;
delay(100);
P3_7 = 1;
P3_7 = 0;
P3_5 = 1;
P3_5 = 0;
TR0 = 1;
}
}

void main() {
TMOD = 0x01; // Timer 0 mode 1
TH0 = 0xFE;
TL0 = 0xFE;
TCON |= 0x10; // TR0 = 1
ET0 = 1; // Enable Timer 0 interrupt
EA = 1; // Enable global interrupts
TR0 = 1;

P3_5 = 1; P3_5 = 0;
P3_7 = 1; P3_7 = 0;

while (1) {
send();
}
}
EXP 5A
#include <REG51.H>

void UART_init() {
SCON = 0x50; // 8-bit UART, REN enabled
TMOD = 0x20; // Timer1 in Mode 2
TH1 = 0xFD; // Baud rate 9600
TL1 = 0xFD;
TR1 = 1; // Start Timer1
}

void UART_sendchar(unsigned char ch) {


SBUF = ch;
while (TI == 0); // Wait until transmission done
TI = 0;
}

void UART_sendstring(char *s) {


while (*s) {
UART_sendchar(*s++);
}
}

void UART_receive(char *buff, unsigned char m) {


unsigned char i = 0;
while (i < m - 1) {
while (!RI); // Wait for character
buff[i] = SBUF;
RI = 0;
if (buff[i] == '\n' || buff[i] == '\r') {
break;
}
i++;
}
buf f[i] = '\0'; // Null terminate
}

void main() {
char rs[50];
UART_init();
UART_sendstring("Ready\r\n");

while (1) {
UART_sendstring("Enter: ");
UART_receive(rs, 50);
UART_sendstring("\r\nYou typed: ");
UART_sendstring(rs);
UART_sendstring("\r\n");
}
}
EXP 5B
#include <REG51.H>

char r[50];
int i = 0;
bit msg_received = 0;

void init() {
SCON = 0x50; // 8-bit UART, REN enabled
TMOD = 0x20; // Timer1 mode 2
TH1 = 0xFD; // Baud 9600 (11.0592 MHz)
TL1 = 0xFD;
// PCON = 0x00; // Optional, don't set SMOD unless
needed
TR1 = 1;
IE = 0x90; // Enable serial interrupt and global
interrupt
}

void sendchar(unsigned char ch) {


SBUF = ch;
while (TI == 0);
TI = 0;
}

void sendstring(char *s) {


while (*s) {
sendchar(*s++);
}
}

void UART_interrupt(void) interrupt 4 {


if (RI) {
char ch = SBUF;
RI = 0;
if (ch != '\n' && ch != '\r') {
r[i++] = ch;
} else {
r[i] = '\0';
i = 0;
msg_received = 1;
}
}
}

void main() {
init();
sendstring("read\r\n");

while (1) {
if (msg_received) {
sendstring("You typed: ");
sendstring(r);
sendstring("\r\n");
msg_received = 0;
}
}
}
EXP 6A
#include<REG51.H>
unsigned char a;
void init(){
SCON=0x50;
TMOD=0x20;
TH1=0xFD;
TL1=0xFD;
TR1=0x01;
}
void receive(){

while(!RI);
a=SBUF;
RI=0;

}
void send(unsigned char x){
SBUF=x;
while(!TI);
TI=0;
}

void main(){
init();
receive();
send(a);
switch(a){
case '1':
P0=0X55;
break;
case '2':
P0=0XAA;
break;
default:
P0=0;
break;
}
}
EXP 6B
#include<reg51.h>
sbit P0_0=P0^0;
sbit P0_1=P0^1;
void UART_init(void);
void UART_tx(char *str);
void main(){
UART_init();
IT0=1;
IT1=1;
EX0=1;
EX1=1;
EA=1;
UART_tx("System ready");
while(1){
if(P0_0==0){
UART_tx("Port0_0 is active");
}
if(P0_1==0x00){
UART_tx("Port0_1 is active");
}
}
}
void UART_tx(char *str){
while(*str){
SBUF=*str++;
while(!TI);
TI=0;
}
}
void ISR_INT0(void) interrupt 0{
UART_tx("INT0 is active");
}
void ISR_INT1(void) interrupt 1{
UART_tx("INT1 is active");
}

You might also like