0% found this document useful (0 votes)
48 views12 pages

All Code Micro

The document contains C code for several microcontroller programs including memory transfer, hexadecimal/binary display, BCD display, ASCII display, flashing LED, seven segment display, timer interrupt, LCD display, and UART communication. The programs demonstrate basic microcontroller operations like I/O, timing, and interrupts. Memory is transferred between arrays, values are displayed on ports in various formats, an LED is flashed, numbers are shown on a seven segment display, a timer interrupt is used to toggle an LED, text is written to an LCD, and data is transmitted via UART.

Uploaded by

20112075
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)
48 views12 pages

All Code Micro

The document contains C code for several microcontroller programs including memory transfer, hexadecimal/binary display, BCD display, ASCII display, flashing LED, seven segment display, timer interrupt, LCD display, and UART communication. The programs demonstrate basic microcontroller operations like I/O, timing, and interrupts. Memory is transferred between arrays, values are displayed on ports in various formats, an LED is flashed, numbers are shown on a seven segment display, a timer interrupt is used to toggle an LED, text is written to an LCD, and data is transmitted via UART.

Uploaded by

20112075
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/ 12

//Memory transfer

#include<reg51.h>

void main(void){

unsigned char source[10]={0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20};

unsigned char destination[10];

int i;

for(i=0;i<10;i++){

destination[i]=source[i];

while(1);

//Hex/Binary

#include<reg51.h>

void delay(void);

void main(void){

unsigned char
Bnum[16]={0x00,0x01,0x04,0x05,0x10,0x11,0x14,0x15,0x40,0x41,0x44,0x45,0x50,0x51,0x54,0x55};

while(1){

int k;

for(k=0;k<16;k++){

P1=Bnum;

delay();

void delay(void){

int i;

for(i=0;i<10;i++){

int j;

for(j=0;j<10000;j++); }}
//BCD

#include<reg51.h>

void delay(void);

void main(void){

unsigned char Bnum[10]={0x00,0x01,0x04,0x05,0x10,0x11,0x14,0x15,0x40,0x41};

while(1){

int k;

for(k=0;k<10;k++){

P1=Bnum;

delay();

void delay(void){

int i;

for(i=0;i<10;i++){

int j;

for(j=0;j<10000;j++);

}
//ASCII

#include<reg51.h>

void delay(void);

void main(void){

unsigned char mynum[]="0123456ABCD";

while(1){

int k;

for(k=0;k<16;k++){

P1=mynum[k];

delay();

void delay(void){

int i;

for(i=0;i<10;i++){

int j;

for(j=0;j<10000;j++);

//Flashing LED

#include<reg51.h>

void delay(void);

void main(void){

while(1){

P1=0x00;

delay();

P1=0xFF;

delay();

}
void delay(void){

int i;

for(i=0;i<10;i++){

int j;

for(j=0;j<10000;j++){

//Seven Segment

#include <reg51.h>

#define SegOne 0x02

#define SegTwo 0x01

void DELAY_ms(unsigned int ms_Count)

unsigned int i,j;

for(i=0;i<ms_Count;i++)

for(j=0;j<100;j++);

int main()

char seg_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

int cnt, num, temp,i;

while (1)
{

for (cnt = 0x00; cnt <= 99; cnt++) // loop to display 0-99

for (i = 0; i < 100; i++)

num = cnt;

temp = num % 10;

P2 = SegOne;

P1 = seg_code[temp];

DELAY_ms(10);

temp = num / 10;

P2 = SegTwo;

P1 = seg_code[temp];

DELAY_ms(10);

}
//Pract_4_LBR
#include<p18f4550.h>
extern void _startup(void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x1000
void _reset(void){
_asm goto _startup _endasm
}
#pragma code
#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008
void _high_ISR(void){
//_asm goto High_ISR_endasm
}
#pragma code _LOW_INTERRUPT_VECTOR = 0x1018
void _low_ISR(void){
// _asm goto Low_ISR _endasm
}
#pragma code

void delay(int n){


int i,j;
for(i=0;i<n;i++)
for(j=0;j<1000;j++);
}
void main(void){
// tris sets the input output port
TRISBbits.RB4=0; //output port
TRISBbits.RB5=0; //output port
TRISBbits.RB6=0; //output port
TRISBbits.RB7=0; //output port
TRISBbits.RB0=1; //input port
TRISBbits.RB1=1; //input port
TRISCbits.RC0=0; //buzzer
TRISCbits.RC1=0; //relay
while(1){
if(PORTBbits.RB0==0){
PORTCbits.RC0=1;
PORTCbits.RC1=1;
while(1){
PORTB = 0x10;
delay(100);
PORTB = 0x20;
delay(100);
PORTB = 0x40;
delay(100);
PORTB = 0x80;
delay(100);
if(PORTBbits.RB1==0){
break;
}
}
}
else if(PORTBbits.RB1==0){
PORTCbits.RC0=0;
PORTCbits.RC1=0;
while(1){
PORTB = 0x80;
delay(100);
PORTB = 0x40;
delay(100);
PORTB = 0x20;
delay(100);
PORTB = 0x10;
delay(100);
if(PORTBbits.RB0==0){
break;
}
}
}
}
}

//Pract_5_LCD and Pin


//Embedded C Program for LCD and PIC 18F4550 interfacing
#include<p18f4550.h>
void LCD_command(unsigned char value);//Function Prototype declaration
void lcddata(unsigned char value);
void msdelay(unsigned int itime);
void LCD_write_string(static char *str);

#define ldata PORTD //Declare ldata variable for PORTD


#define rs PORTEbits.RE0 //Declare rs variable for pin RE0
#define rw PORTEbits.RE1 //Declare rw variable for pin RE1
#define en PORTEbits.RE2 //Declare en variable for pin RE2

void main()
{
unsigned char i;
unsigned char msg1[ ]="PVG";
unsigned char msg2[ ]="COET PUNE";
TRISD = 0x00; //Set direction of PORTD as an output (Data lines of
LCD)
ADCON1=0X0F; // CHANNELS OF INTERNAL ADC used as a digital pin
TRISE=0X00; //set direction of PORTE as an output (Control lines of
LCD)
msdelay(50);

LCD_write_string(msg1); //Display string "SPPU"


msdelay(500);
LCD_command(0XC4); // Force cursor to blink on second row 5th position
LCD_write_string(msg2); //Display string "UNIVERSITY"
msdelay(500);
}

void LCD_init(void)
{
LCD_command(0x38); //8-bit mode, 2 Line, 5x7 Dots matrix for each
character display
msdelay(50);
LCD_command(0x0C); //Display ON, Cursor ON
msdelay(15);
LCD_command(0x01); //clear Display screen
msdelay(15);
LCD_command(0x06); //Increment cursor(shift right)
msdelay(15);
LCD_command(0X80); // Force cursor to blink on first row 1st position
msdelay(15);
}
void LCD_command (unsigned char value)
{
ldata=value; //Send the command value to PORTD
rs=0; //selection of command register of LCD
rw=0;
en=1; //Generate High to Low pulse on Enable pin
msdelay(1);
en=0;
}
void LCD_write_string(static char *str)
{
int i = 0;
while (str[i] != 0)
{
lcddata(str[i]);
msdelay(15);
i++;
}
}
void lcddata (unsigned char value)
{
ldata=value; //Send the command value to PORTD
rs=1; //selection of DATA register of LCD
rw=0;
en=1; //Generate High to Low pulse on Enable pin
msdelay(1);
en=0;
}
void msdelay (unsigned int itime)
{
int k,j;
for(k=0;k<itime;k++)
for(j=0;j<135;j++);
}

//Pract_6
#include<p18f4550.h>
#include<stdlib.h>
extern void _startup (void);
void timer_isr(void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x1000
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code
//The program execution comes to this point when a timer interrupt is
generated
#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008
void high_ISR (void)
{
_asm goto timer_isr _endasm //The program is relocated to execute
the interrupt routine timer_isr
}
#pragma code _LOW_INTERRUPT_VECTOR = 0x1018
void low_ISR (void)
{
}
#pragma code
// This function is executed as soon as the timer interrupt is generated
due to timer overflow
#pragma interrupt timer_isr
void timer_isr(void)
{
// TMR0H = 0XF6; // Reloading the timer values after overflow
// TMR0L = 0X3C;
TMR0H = 0XFF; // Reloading the timer values after overflow
TMR0L = 0XFF;
PORTB = ~PORTB; //Toggle the PORTB led outputs RB0 - RB3
INTCONbits.TMR0IF = 0;//Resetting the timer overflow interrupt flag
}
//void delay(int i)
//{
//T0CON=0x00;
//TMR0H=0xFF;
//INTCONbits.TMR0IF=0;
//INTCONbits.TMR0IE=1;
//INTCONbits.GIE=1;
//T0CONbits.TMR0ON=1;
//}
void main()
{
ADCON1 = 0x0F; //Configuring the PORTB pins as digital I/O
TRISB = 0; //Configuring the LED port pins as outputs
PORTB = 0xFF; //Setting the initial value of the LED's after reset
T0CON = 0x00; //Set the timer to 16-bit mode,internal instruction cycle
clock,no
prescaler
// TMR0H = 0xFD;
//TMR0L = 0xA8;
INTCONbits.TMR0IF =0; // Clear Timer0 overflow flag
INTCONbits.TMR0IE =1; // TMR0 interrupt enabled
INTCONbits.GIE =1; // Global interrupt enabled
T0CONbits.TMR0ON =1; // Start timer0
while(1); //Program execution stays here untill the timer overflow
interrupt is generated
}

//Pract_7
/**************************************************/
/** I N C L U D E S **/
#include<p18f4550.h>
/** V A R I A B L E S ***********************/
#pragma udata
#pragma idata
unsigned char String[]={"WELCOME \n\rPress any key from PC\n\r"};
unsigned char String1[]={"\n\rUART Tested\n\r"};
/** P R I V A T E P R O T O T Y P E S
***************************************/
extern void _startup (void); // See c018i.c in your C18 compiler
dir
void High_ISR(void);
void Low_ISR(void);
/** V E C T O R R E M A P P I N G
*******************************************/
extern void _startup (void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x1000
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code

#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008


void _high_ISR (void)
{

#pragma code _LOW_INTERRUPT_VECTOR = 0x1018


void _low_ISR (void)
{

}
#pragma code

/** D E C L A R A T I O N S
**************************************************/
#pragma code

//#pragma config WDT=OFF

void TXbyte(char data)


{
while(TXSTAbits.TRMT==0); //wait tiiltransmit buffer in not empty
TXREG = data; // Transmit Data
}//end TXbyte

void main()
{
unsigned char temp;
unsigned char i=0;
SSPCON1 = 0; // Make sure SPI is disabled //Refer Datasheet
TRISCbits.TRISC7=1; // RX
TRISCbits.TRISC6=0; // TX
SPBRG = 0x71;
SPBRGH = 0x02;// 0x0271 for fosc :48MHz -> 19200 baud XTAL=20MHz,
TXSTA = 0x24; // TX enable BRGH=1
RCSTA = 0x90; // continuous RX
BAUDCON = 0x08; // BRG16 = 1
temp = RCREG; // Empty buffer
temp = RCREG; // Empty buffer

for(i=0;String[i]!='\0';i++)
{
TXbyte(String[i]);
}
while(PIR1bits.RCIF==0); //Wait util data from PC is received
for(i=0;String1[i]!='\0';i++)
{
TXbyte(String1[i]);
}
while(1); //loop forever
}

//Pract_8
/** I N C L U D E S ********/

#include<p18f4550.h>

/** P R I V A T E P R O T O T Y P E S
***************************************/

void myMsDelay (unsigned int time);

/** V E C T O R R E M A P P I N G
*******************************************/

extern void _startup (void); // See c018i.c


#pragma code _RESET_INTERRUPT_VECTOR = 0x1000
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code

#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008


void _high_ISR (void)
{
//_asm goto High_ISR _endasm
}

#pragma code _LOW_INTERRUPT_VECTOR = 0x1018


void _low_ISR (void)
{
// _asm goto Low_ISR _endasm
}

#pragma code

/** D E C L A R A T I O N S
**************************************************/

#pragma code

void main()
{
TRISCbits.TRISC0 = 0 ; // DC Motor IN1
TRISCbits.TRISC1 = 0 ; // DC Motor IN2
TRISCbits.TRISC2 = 0 ; // CCP2 PWM
output, DC motor enable
PR2 = 0x4A; //10khz // set PWM period to
Maximum value
CCPR1L = 0x12; // Initalise PWM duty cycle to
00
CCP1CON = 0x2C; // Configure CCP1CON as
explained above.
T2CON = 0x07;
PORTCbits.RC0 = 0;
PORTCbits.RC1 = 1;
while(1)
{

CCPR1L = 0x12; //25% Duty Cycle


myMsDelay(1000);
CCP1CON = 0x0C; // Configure CCP1CON as
explained above.
CCPR1L = 0x25; //50% Duty Cycle
myMsDelay(1000);
CCP1CON = 0x2C; // Configure CCP1CON as
explained above.
CCPR1L = 0x37; //75% Duty Cycle
myMsDelay(1000);

void myMsDelay (unsigned int time)


{
unsigned int i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 710; j++);/*Calibrated for a 1 ms delay
in MPLAB*/
}

You might also like