( CTV Angels Demons ) Mạch đèn giao thông hiển thị LCD 16x2 lập trình bằng ATmega16 + Biên dịch AVRstudio 6.
Code Main:
/*
* den_giao_thong.c
*
* Created: 27/11/2013 6:12:57 CH
* Author: Entertainment
*/
#ifndef F_CPU
#define F_CPU 12000000UL // 1 MHz
#define TIMER_PRESCALE
1024
#define TIMER1_INTERVAL
(F_CPU/TIMER_PRESCALE)
#define TIMER1PRESCALE
0x05
///< timer 1 prescaler default
#define TIMER_PRESCALE_MASK
0x07
///< Timer Prescaler Bit-Mask
#endif
//-----------------------------
#include <avr/io.h>
#include <avr/interrupt.h>
#include "myLCD.h"
#include "delay.h"
volatile uint8_t val=0; //khai bao 1 bien tam val va khoi tao =0
static volatile unsigned int Counter1s;
//char dis[10];
void timer1Init(void)
{
TCCR1B = (TCCR1B & ~TIMER_PRESCALE_MASK) | TIMER1PRESCALE;
// set prescaler
TCNT1H = 0;
// reset TCNT1
TCNT1L = 0;
TIMSK |= (1<<TOIE1);
// enable TCNT1 overflow
TCNT1 = 0xFFFF - TIMER1_INTERVAL;
//TCNT1=49910; //gan gia tri khoi tao cho T/C1
}
void trang_thai_1(void)
{
char dis[5];
if(Counter1s<=40){
clr_LCD();
move_LCD(1,3);
print_LCD("Den Xanh - Do");
sprintf(dis,"%i", Counter1s);
move_LCD(2,3);
print_LCD("Thoi Gian: ");
print_LCD(dis);
PORTB &= ~((1 << PB1) | (1 << PB2)) ;
PORTD &= ~((1 << PD0) | (1 << PD1)) ;
//----------------------------
PORTB |= (1 << PB0); //xanh va do
PORTD |= (1 << PD2);
}
}
void trang_thai_2(void)
{
char dis[5];
if((Counter1s>40)&&(Counter1s<=43)){
clr_LCD();
move_LCD(1,3);
print_LCD("Den Vang - Do");
val = (Counter1s - 40);
sprintf(dis,"%i",val);
move_LCD(2,3);
print_LCD("Thoi Gian: ");
print_LCD(dis);
PORTB &= ~((1 << PB0) | (1 << PB2)) ;
PORTD &= ~((1 << PD0) | (1 << PD1)) ;
//-----------------------------------------
PORTB |= (1 << PB1); //vang va do
PORTD |= (1 << PD2);
}
}
void trang_thai_3(void)
{
char dis[5];
if((Counter1s>43)&&(Counter1s<=73)){
clr_LCD();
move_LCD(1,3);
print_LCD("Den Do - Xanh");
val = (Counter1s - 43);
sprintf(dis,"%i", val);
move_LCD(2,3);
print_LCD("Thoi Gian: ");
print_LCD(dis);
PORTB &= ~((1 << PB0) | (1 << PB1)) ;
PORTD &= ~((1 << PD1) | (1 << PD2)) ;
//-------------------------
PORTB |= (1 << PB2); //do va xanh
PORTD |= (1 << PD0);
}
}
void trang_thai_4(void)
{
char dis[5];
if((Counter1s>73)&&(Counter1s<=76)){
clr_LCD();
move_LCD(1,3);
print_LCD("Den Do - Vang");
val = (Counter1s - 73);
sprintf(dis,"%i", val);
move_LCD(2,3);
print_LCD("Thoi Gian: ");
print_LCD(dis);
PORTB &= ~((1 << PB0) | (1 << PB1)) ;
PORTD &= ~((1 << PD0) | (1 << PD2)) ;
//--------------------------------
PORTB |= (1 << PB2); //do va vang
PORTD |= (1 << PD1);
}
}
int main(void){
DDRA=0xFF; //PORTA la output PORT
PORTA=0x00;
DDRB=0xFF; //PORTB la output PORT
PORTB=0x00;
DDRD=0xFF; //PORTD la output PORT
PORTD=0x00;
timer1Init();
sei(); //set bit I cho phep ngat toan cuc
init_LCD();
clr_LCD();
//putChar_LCD(' ');
putChar_LCD(' ');
putChar_LCD('D');
print_LCD("emo of control");
move_LCD(2,2);
print_LCD("Den Giao Thong");
_delay_ms(3000);
while (1){ //v?ng l?p vô t?n
// Nothing Here :D
}
return 0;
}
//trinh phuc vu ngat tran T/C1
ISR (TIMER1_OVF_vect){
TCNT1 = 0xFFFF - TIMER1_INTERVAL;
Counter1s++;
if (Counter1s==77) Counter1s=1;
PORTA =Counter1s; //xuat gia tri ra PORTA
trang_thai_1();
trang_thai_2();
trang_thai_3();
trang_thai_4();
}
( Tải Full)
Echipkool.com - Điện Tử | Tin Học - Chia sẻ kiến thức - Kết nối đam mê điện tử