COMMON PART
#define F_CPU 14745600UL
#include <avr/io.h>
#include <util/delay.h>
#define sbi(PORTX, BITX) PORTX |=(1<<BITX)
#define cbi(PORTX, BITX) PORTX &= ~(1<<BITX)
#define TLCD_EN { cbi(PORTB,2); sbi(PORTB,2); }
#define DATA PORTC
void Port_Init(void){
DDRB = 0xff;
DDRC = 0xff;
}
void E_Pulse(void){
sbi(PORTB,2);
_delay_ms(5);
cbi(PORTB,2);
}
void TLCD_DATA(unsigned char data){
DATA = data;
TLCD_EN;
}
void lcd_char(char s){
sbi(PORTB,0);
TLCD_DATA(s);
E_Pulse();
}
void Func_Set(void){
cbi(PORTB,0);
cbi(PORTB,1);
TLCD_DATA(0x38);
E_Pulse();
}
void Init_LCD(void){
cbi(PORTB,2);
_delay_ms(15);
Func_Set();
_delay_ms(10);
Func_Set();
_delay_us(150);
Func_Set();
TLCD_DATA(0x0f);
E_Pulse();
TLCD_DATA(0x06);
E_Pulse();
TLCD_DATA(0x01);
E_Pulse();
}
void clrscr(void){
cbi(PORTB,0);
cbi(PORTB,1);
TLCD_DATA(0x01);
E_Pulse();
_delay_ms(10);
}
void lcd_disp(char x, char y){
cbi(PORTB,0);
cbi(PORTB,1);
if(y==0)
TLCD_DATA(x+0x80);
else if(y==1)
TLCD_DATA(x+0xc0);
E_Pulse();
}
void lcd(char x, char y, char *str){
lcd_disp(x, y);
while(*str)
lcd_char(*str++);
}
Servo motor
int main(){
unsigned int i;
sbi(DDRB,7);
Port_Init();
Init_LCD();
while(1){
for(i=0; i<20; i++){
lcd(0,0,"Servo angle= +90");
sbi(PORTB,7);
_delay_us(700);
cbi(PORTB,7);
_delay_us(19300);
}
clrscr();
for(i=0; i<20; i++){
lcd(0,0,"Servo angle= 0");
sbi(PORTB,7);
_delay_us(1500);
cbi(PORTB,7);
_delay_us(18500);
}
clrscr();
for(i=0; i<20; i++){
lcd(0,0,"Servo angle= -90");
sbi(PORTB,7);
_delay_us(2300);
cbi(PORTB,7);
_delay_us(17700);
}
clrscr();
}
return 0;
}
DC motor
int main(){
Port_Init();
Init_LCD();
DDRB = 0xff;
DDRF = 0x01;
while(1){
PORTF = 0x01;
cbi(PORTB,6);
sbi(PORTB,7);
lcd(0,0,"DC: Clockwise");
_delay_ms(5000);
PORTF = 0x00;
clrscr();
lcd(0,0,"DC: Stopped ");
_delay_ms(2000);
PORTF = 0x01;
sbi(PORTB,6);
cbi(PORTB,7);
lcd(0,0,"DC: AntiClockwise");
_delay_ms(5000);
PORTF = 0x00;
clrscr();
lcd(0,0,"DC: Stopped ");
_delay_ms(2000);
}
}
LED
int main(){
Port_Init();
Init_LCD();
DDRD |= (1 << PD0);
while(1){
sbi(PORTD, 0);
lcd(0, 0, "LED: ON");
_delay_ms(2000);
clrscr();
cbi(PORTD, 0);
lcd(0, 0, "LED: OFF");
_delay_ms(2000);
clrscr();
}
}