Diễn đàn codientu.org chia sẻ code đèn giao thông có tùy chỉnh Setup thời gian khá hay. Chip đem chia sẻ lại để bạn nào cần làm " Đồ Ăn" lấy về xài. Code Compile by Codevison - Mạch PCB mở bằng ALtium Designer.
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 6/5/2013
Author : NeVaDa
Company :
Comments:
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
#define mode PIND.7 //khai bao cac phim nhan cai dat
#define down PIND.5
#define up PIND.6
#define mode1 PIND.4
#define sw PIND.3
#define led1 PORTA.0
#define led2 PORTA.1
#define led3 PORTA.2
#define led4 PORTA.3
#define vang1 PORTC.1
#define do1 PORTC.2
#define xanh1 PORTC.0
#define vang2 PORTC.4
#define xanh2 PORTC.3
#define do2 PORTC.5
unsigned char so[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0X82,0XF8,0x80,0x90};
int k,giay;
//char den1,den2;
unsigned int trangthai = 1;
unsigned int td1 = 30,td2=30,tv1=5,tv2=5,tx1=25,tx2=25;
//unsigned int td1,td2,tv1,tv2,tx1,tx2;
unsigned int td11 = 30,td22=30,tv11=5,tv22=5,tx11=25,tx22=25;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0x06;
k++;
if(k>=40){giay++;k=0; }
if(giay>90){giay=0;}
// Place your code here
}
void hienthi(unsigned char x, unsigned char y)
{
unsigned char chuc1,dv1,chuc2,dv2;
chuc1=x/10;
dv1=x%10;
chuc2=y/10;
dv2=y%10;
led1=0;led2=0;led3=0;led4=0;
led1=1;led2=0;led3=0;led4=0;
PORTB=so[chuc1];
delay_ms(5);
led1=0;led2=0;led3=0;led4=0;
led1=0;led2=1;led3=0;led4=0;
PORTB=so[dv1];
delay_ms(5);
led1=0;led2=0;led3=0;led4=0;
led1=0;led2=0;led3=1;led4=0;
PORTB=so[chuc2];
delay_ms(5);
led1=0;led2=0;led3=0;led4=0;
led1=0;led2=0;led3=0;led4=1;
PORTB=so[dv2];
delay_ms(5);
}
void chinhthoigian()
{ int i;
int k=1;
while(sw==0)
{
if(k==1)
{
PORTC=255;xanh1 = 0;
if(up==0)
{
while(up==0){} tx11=tx11+1;if(tx11==100)tx11=0; td22=tx11+tv11;
for(i=0;i<20;i++)hienthi(tx11,0);
}
if(down==0)
{
while(down==0){} tx11=tx11-1;if(tx11==0)tx1=99; td22=tx11+tv11;
for(i=0;i<20;i++)hienthi(tx11,0);
}
if(mode==0){while(mode==0){} k=2;}
if(mode1==0){while(mode1==0){} k=5;}
}
if(k==2)
{
PORTC=255;vang1 = 0;
if(up==0)
{
while(up==0){} tv11=tv11+1;if(tv11==100)tv11=0; td22=tx11+tv11;
for(i=0;i<20;i++)hienthi(tv11,0);
}
if(down==0)
{
while(down==0){} tx11=tv11-1;if(tv11==0)tv11=99; td22=tx11+tv11;
for(i=0;i<20;i++)hienthi(tv1,0);
}
if(mode==0){while(mode==0){} k=3;}
if(mode1==0){while(mode1==0){} k=5;}
}
if(k==3)
{
PORTC=255;xanh2 = 0;
if(up==0)
{
while(up==0){} tx22=tx22+1;if(tx22==100)tx22=0; td11=tx22+tv22;
for(i=0;i<20;i++)hienthi(0,tx22);
}
if(down==0)
{
while(down==0){} tx22=tx22-1;if(tx22==0)tx2=99; td11=tx22+tv22;
for(i=0;i<20;i++)hienthi(0,tx22);
}
if(mode==0){while(mode==0){} k=4;}
if(mode1==0){while(mode1==0){} k=5;}
}
if(k==4)
{
PORTC=255;vang2 = 0;
if(up==0)
{
while(up==0){} tv22=tv22+1;if(tv22==100)tv22=0; td11=tx22+tv22;
for(i=0;i<20;i++)hienthi(0,tv22);
}
if(down==0)
{
while(down==0){} tv22=tv22-1;if(tv22==0)tv22=99; td11=tx22+tv22;
for(i=0;i<300;i++)hienthi(0,tv22);
}
if(mode==0){while(mode==0){} k=2;}
if(mode1==0){while(mode1==0){} k=5;}
}
if(k==5)
{
PORTC=255; do1=do2=0; hienthi(td11,td22);
tx1=tx11;tx2=tx22;td1=td11;td2=td22;tv1=tv11;tv2=tv22;
//break;
}
}
}
// Declare your global variables here
void xanh1do2()
{
xanh1=0; xanh2=1; do1=1; do2=0; vang1=1; vang2=1;
hienthi(tx1,td2);
}
void vang1do2()
{
xanh1=1; xanh2=1; do1=1; do2=0; vang1=0; vang2=1;
hienthi(tv1,td2);
}
void do1xanh2()
{
xanh1=1; xanh2=0; do1=0; do2=1; vang1=1; vang2=1;
hienthi(td1,tx2);
}
void do1vang2()
{
xanh1=1; xanh2=1; do1=0; do2=1; vang1=1; vang2=0;
hienthi(td1,tv2);
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0xFF;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0xFF;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x02;
TCNT0=0x06;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
if(sw==1)
{
if(giay==89) // sau 1 giay
{
switch (trangthai)
{
case 1:
tx1=tx1-1;td2=td2-1;
if(tx1==0){tx1=25;trangthai=2;}
break;
case 2:
tv1=tv1-1;td2=td2-1;
if(tv1==0){tv1=5;td2=30;trangthai=3;}
break;
case 3:
td1=td1-1;tx2=tx2-1;
if(tx2==0){tx2=25;trangthai=4;}
break;
case 4:
td1=td1-1;tv2=tv2-1;
if(tv2==0){tv2=5;td1=30;trangthai=1;}
break;
}
giay =0;
}
// goi cac ham de hien thi
switch (trangthai)
{
case 1:
xanh1do2();
break;
case 2:
vang1do2();
break;
case 3:
do1xanh2();
break;
case 4:
do1vang2();
break;
}
}
else
{
PORTC=255; //tat het
chinhthoigian();
}
}
}
( Full + Code C + Schematic Altium Designer)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút hiện ra ở góc phải phía trên, nhấn vào để đến trang download
Nguồn codientu.org - Ươm mầm sáng tạo - Kết nối đam mê
Code:
/*****************************************************This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 6/5/2013
Author : NeVaDa
Company :
Comments:
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
#define mode PIND.7 //khai bao cac phim nhan cai dat
#define down PIND.5
#define up PIND.6
#define mode1 PIND.4
#define sw PIND.3
#define led1 PORTA.0
#define led2 PORTA.1
#define led3 PORTA.2
#define led4 PORTA.3
#define vang1 PORTC.1
#define do1 PORTC.2
#define xanh1 PORTC.0
#define vang2 PORTC.4
#define xanh2 PORTC.3
#define do2 PORTC.5
unsigned char so[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0X82,0XF8,0x80,0x90};
int k,giay;
//char den1,den2;
unsigned int trangthai = 1;
unsigned int td1 = 30,td2=30,tv1=5,tv2=5,tx1=25,tx2=25;
//unsigned int td1,td2,tv1,tv2,tx1,tx2;
unsigned int td11 = 30,td22=30,tv11=5,tv22=5,tx11=25,tx22=25;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0x06;
k++;
if(k>=40){giay++;k=0; }
if(giay>90){giay=0;}
// Place your code here
}
void hienthi(unsigned char x, unsigned char y)
{
unsigned char chuc1,dv1,chuc2,dv2;
chuc1=x/10;
dv1=x%10;
chuc2=y/10;
dv2=y%10;
led1=0;led2=0;led3=0;led4=0;
led1=1;led2=0;led3=0;led4=0;
PORTB=so[chuc1];
delay_ms(5);
led1=0;led2=0;led3=0;led4=0;
led1=0;led2=1;led3=0;led4=0;
PORTB=so[dv1];
delay_ms(5);
led1=0;led2=0;led3=0;led4=0;
led1=0;led2=0;led3=1;led4=0;
PORTB=so[chuc2];
delay_ms(5);
led1=0;led2=0;led3=0;led4=0;
led1=0;led2=0;led3=0;led4=1;
PORTB=so[dv2];
delay_ms(5);
}
void chinhthoigian()
{ int i;
int k=1;
while(sw==0)
{
if(k==1)
{
PORTC=255;xanh1 = 0;
if(up==0)
{
while(up==0){} tx11=tx11+1;if(tx11==100)tx11=0; td22=tx11+tv11;
for(i=0;i<20;i++)hienthi(tx11,0);
}
if(down==0)
{
while(down==0){} tx11=tx11-1;if(tx11==0)tx1=99; td22=tx11+tv11;
for(i=0;i<20;i++)hienthi(tx11,0);
}
if(mode==0){while(mode==0){} k=2;}
if(mode1==0){while(mode1==0){} k=5;}
}
if(k==2)
{
PORTC=255;vang1 = 0;
if(up==0)
{
while(up==0){} tv11=tv11+1;if(tv11==100)tv11=0; td22=tx11+tv11;
for(i=0;i<20;i++)hienthi(tv11,0);
}
if(down==0)
{
while(down==0){} tx11=tv11-1;if(tv11==0)tv11=99; td22=tx11+tv11;
for(i=0;i<20;i++)hienthi(tv1,0);
}
if(mode==0){while(mode==0){} k=3;}
if(mode1==0){while(mode1==0){} k=5;}
}
if(k==3)
{
PORTC=255;xanh2 = 0;
if(up==0)
{
while(up==0){} tx22=tx22+1;if(tx22==100)tx22=0; td11=tx22+tv22;
for(i=0;i<20;i++)hienthi(0,tx22);
}
if(down==0)
{
while(down==0){} tx22=tx22-1;if(tx22==0)tx2=99; td11=tx22+tv22;
for(i=0;i<20;i++)hienthi(0,tx22);
}
if(mode==0){while(mode==0){} k=4;}
if(mode1==0){while(mode1==0){} k=5;}
}
if(k==4)
{
PORTC=255;vang2 = 0;
if(up==0)
{
while(up==0){} tv22=tv22+1;if(tv22==100)tv22=0; td11=tx22+tv22;
for(i=0;i<20;i++)hienthi(0,tv22);
}
if(down==0)
{
while(down==0){} tv22=tv22-1;if(tv22==0)tv22=99; td11=tx22+tv22;
for(i=0;i<300;i++)hienthi(0,tv22);
}
if(mode==0){while(mode==0){} k=2;}
if(mode1==0){while(mode1==0){} k=5;}
}
if(k==5)
{
PORTC=255; do1=do2=0; hienthi(td11,td22);
tx1=tx11;tx2=tx22;td1=td11;td2=td22;tv1=tv11;tv2=tv22;
//break;
}
}
}
// Declare your global variables here
void xanh1do2()
{
xanh1=0; xanh2=1; do1=1; do2=0; vang1=1; vang2=1;
hienthi(tx1,td2);
}
void vang1do2()
{
xanh1=1; xanh2=1; do1=1; do2=0; vang1=0; vang2=1;
hienthi(tv1,td2);
}
void do1xanh2()
{
xanh1=1; xanh2=0; do1=0; do2=1; vang1=1; vang2=1;
hienthi(td1,tx2);
}
void do1vang2()
{
xanh1=1; xanh2=1; do1=0; do2=1; vang1=1; vang2=0;
hienthi(td1,tv2);
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0xFF;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0xFF;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x02;
TCNT0=0x06;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
if(sw==1)
{
if(giay==89) // sau 1 giay
{
switch (trangthai)
{
case 1:
tx1=tx1-1;td2=td2-1;
if(tx1==0){tx1=25;trangthai=2;}
break;
case 2:
tv1=tv1-1;td2=td2-1;
if(tv1==0){tv1=5;td2=30;trangthai=3;}
break;
case 3:
td1=td1-1;tx2=tx2-1;
if(tx2==0){tx2=25;trangthai=4;}
break;
case 4:
td1=td1-1;tv2=tv2-1;
if(tv2==0){tv2=5;td1=30;trangthai=1;}
break;
}
giay =0;
}
// goi cac ham de hien thi
switch (trangthai)
{
case 1:
xanh1do2();
break;
case 2:
vang1do2();
break;
case 3:
do1xanh2();
break;
case 4:
do1vang2();
break;
}
}
else
{
PORTC=255; //tat het
chinhthoigian();
}
}
}
( Full + Code C + Schematic Altium Designer)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút hiện ra ở góc phải phía trên, nhấn vào để đến trang download
Nguồn codientu.org - Ươm mầm sáng tạo - Kết nối đam mê
Đăng nhận xét