Vous êtes sur la page 1sur 6

i

APPENDIX
CODING:
1. CODING FOR BLINKING OF LED:
void main()
{
PORTC = 0; // Initialize PORTC
TRISC = 0; // Configure PORTC as output

While(1)
{
PORTC = 0xAA; // gives code 10101010 to PORTC
Delay_ms (1000); // one second delay
}}
2. CODING FOR DISPLAY ON SEVEN SEGMENT DISPLAY:
//seven segment display -common anode for 2 digit
void bcd(unsigned int x);
void delay(unsigned int k) ;
void main()
{
unsigned int i=0;
TRISB=0X00;
while(1)
{
i++;
bcd(i);
delay_ms(20);
}}
void bcd(unsigned int x)
ii


{
unsigned char z,y,a;
if(x<100)
{
for(a=0;a<100;a++)
{
y=(x/10)*6+x;
y=y>>4;
y=y|0xe0;
PORTB=y;
delay(250);
z=(x/10)*6+x;
z=z&0x0f;
z=z|0xd0;
PORTB=z;
delay(250);
}}}
void delay(unsigned int k)
{
while(k!=0)
{
k--;
}}
3. CODING FOR STEPPER MOTOR:
#define l1 PORTB.F0
#define l2 PORTB.F1
#define l3 PORTB.F2
#define l4 PORTB.F3

void forward();
void backward();
iii

void main()
{
PORTC=0XFF;
TRISB=0X00;
while(1)
{
forward();
delay_ms(2000);
backward();
delay_ms(2000);
}
}
void forward() //half stepping
{
unsigned char a;
for(a=0;a<10;a++)
{
l1=1; l2=0; l3=0; l4=0;delay_ms(100);
l1=0; l2=1; l3=0; l4=0;delay_ms(100);
l1=0; l2=0; l3=1; l4=0;delay_ms(100);
l1=0; l2=0; l3=0; l4=1;delay_ms(100);
}}
void backward()
{
unsigned char b;
for(b=0;b<10;b++)
{
l1=0; l2=0; l3=0; l4=1; delay_ms(100);
l1=0; l2=0; l3=1; l4=0; delay_ms(100);
l1=0; l2=1; l3=0; l4=0; delay_ms(100);
l1=1; l2=0; l3=0; l4=0; delay_ms(100);

}}

iv

The above code will rotate the motor first in forward direction and then in reverse
direction.
4. CODING FOR DISPLAY ON LCD:
void main()
{
TRISB = 0; // PORTB is output
Lcd_Init(&PORTB); // Initialize LCD connected to PORTB
Lcd_Cmd(Lcd_CLEAR); // Clear display
Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off
Lcd_Out(1, 5,"HELLO"); // Print text to LCD, 1nd row, 5tH column
}
The above code will display HELLO on LCD.
The functions like Lcd_Init(), Lcd_cmd, Lcd_out are predefined functions in mikroC
which initialize, gives command and displays respectively.
5. CODING FOR SWITCHING ACTION OF PIC PINS:
#define s0 PORTC.F0
#define s1 PORTC.F1
#define s2 PORTC.F2
#define s3 PORTC.F3
#define s4 PORTC.F4
void main()
{
TRISB=0x00; \\ PORT B AS OUTPUT
TRISC=0xff; \\PORT C as input
PORTC=0xff; \\ ALL F/FS TO SET
do
{
if(s0==0) \\1st switch is pressed
{
PORTB=0x80; \\1st LED glows
delay_ms(600);
v

}
if(s1==0) \\2nd switch is pressed
{
PORTB=0x40; \\2nd LED glows
delay_ms(600);
}
if(s2==0) \\3rd switch is pressed
{
PORTB=0x20; \\3rd LED glows
delay_ms(600);
}
if(s3==0) \\4th switch is pressed
{
PORTB=0x10; \\4th LED glows
delay_ms(600);
}
else
{
PORTB=0xff;
}}
while(1);
}
6. CODING FOR SERIAL COMMUNICATION (BETWEEN PC AND
MICROCONTROLLER):
A).To Transmit data:
void main()
{
usart_init(2400);
while(1)
{
usart_write('A');
usart_write('M');
vi

usart_write('A');
usart_write('N');
delay_ms(600);
}
}
2).To Transmit as well as read:
unsigned int i;
void main()
{
usart_init(2400);
while(1)
{
if(usart_data_ready() )
{
i= usart_read();
usart_write('i');
}
usart_write('A');
usart_write('M');
usart_write('A');
usart_write('N');
delay_ms(600);
}

Vous aimerez peut-être aussi