电子抽奖完整项目,基于AVR单片机ATMEG128,包含原理图,源代码及仿真
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- //ICC-AVR application builder
- // Target : M128
- // Crystal: 8.0000Mhz
- #include <iom128v.h>
- #include <macros.h>
- #include <stdlib.h>
- #define bit_read(var,bitno) ((var)<<(7-(bitno))>>7)
- #define TRUE 0xFF
- #define FALSE 0x00
- unsigned int randdata = 0;
- unsigned char wdata,qdata,baidata,sdata,gdata;
- unsigned char temp2,temp3,temp4,temp5,temp6;
- unsigned char TimerFlg = FALSE;
- unsigned char T3Flg = FALSE;
- unsigned char SEGtable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- void delay(int i)
- {
- unsigned char j;
- for(;i>0;i--)
- for(j=111;j>0;j--);
- }
- //统计为1的个数
- unsigned char count(unsigned char n)
- {
- int sum=0;
- while(n)
- {
- sum+=n&0x01;
- n>>=1;
- }
- return sum;
- }
- void initrand(void)
- {
- unsigned char counter=0;
- unsigned char temp = 0;
- PORTC = 0xFF;
- temp = PINC; //读取拨码开关状态
- counter = count(temp);
- srand(counter); //初始化种子
- }
- void port_init(void)
- {
- PORTA = 0x00;
- DDRA = 0x00;
- PORTB = 0x00;
- DDRB = 0xff;
- PORTC = 0x00; //m103 output only
- DDRC = 0x00;
- PORTD = 0x00;
- DDRD = 0x00;
- PORTE = 0x00;
- DDRE = 0xFF;
- PORTF = 0x00;
- DDRF = 0xFF;
- PORTG = 0x00;
- DDRG = 0x00;
- }
- //TIMER3 initialize - prescale:256
- // WGM: 0) Normal, TOP=0xFFFF
- // desired value: 100mSec
- // actual value: 100.000mSec (0.0%)
- void timer3_init(void)
- {
- TCCR3B = 0x00; //stop
- TCNT3H = 0xF3; //setup
- TCNT3L = 0xCB;
- OCR3AH = 0x0C;
- OCR3AL = 0x35;
- OCR3BH = 0x0C;
- OCR3BL = 0x35;
- OCR3CH = 0x0C;
- OCR3CL = 0x35;
- ICR3H = 0x0C;
- ICR3L = 0x35;
- TCCR3A = 0x00;
- TCCR3B = 0x00;
- T3Flg = FALSE;
- }
- #pragma interrupt_handler timer3_ovf_isr:iv_TIM3_OVF
- void timer3_ovf_isr(void)
- {
- //TIMER3 has overflowed
- TCNT3H = 0xF3; //reload counter high value
- TCNT3L = 0xCB; //reload counter low value
- TimerFlg = TRUE; //修改标志位
- PORTB^=BIT(0);
- }
- #pragma interrupt_handler int0_isr:iv_INT0
- void int0_isr(void)
- {
- if(T3Flg == TRUE) //判断当前定时计数器的状态
- {
- TCCR3B = 0x00;
- T3Flg = FALSE;
- PORTB= 0xff;
- }
- else
- {
- initrand(); //初始化种子
- T3Flg = TRUE;
- TCCR3B = 0x04;
- PORTC = 0x00;
- }
- }
- //call this routine to initialize all peripherals
- void init_devices(void)
- {
- //stop errant interrupts until set up
- CLI(); //disable all interrupts
- XDIV = 0x00; //xtal divider
- XMCRA = 0x00; //external memory
- port_init();
- timer3_init();
- MCUCR = 0x00;
- EICRA = 0x00; //extended ext ints
- EICRB = 0x00; //extended ext ints
- EIMSK = 0x01;
- TIMSK = 0x00; //timer interrupt sources
- ETIMSK = 0x04; //extended timer interrupt sources
- SEI(); //re-enable interrupts
- //all peripherals are now initialized
- }
- void WR5952(unsigned char data)
- {
- //PE0-SHCP,PE1-DS,PE2-STCP
- unsigned char j;
- /*SPI发送开始*/
- PORTE&=~(1<<PE2);
- delay(1);
- for(j=0;j<=7;j++)
- {
- PORTE&=~(1<<PE0);
- delay(1);
- if(bit_read(data,j))
- {
- PORTE|=1<<PE1;
- }
- else
- {
- PORTE&=~(1<<PE1);
- }
- delay(1);
- PORTE=1<<PE0;
- delay(1);
- }
- PORTE&=~(1<<PE0);
- delay(1);
- PORTE|=1<<PE2;
- delay(1);
- }
- void WR5953(unsigned char data)
- {
- //PE3-SHCP,PE4-DS,PE5-STCP
- unsigned char j;
- /*SPI发送开始*/
- PORTE&=~(1<<PE5);
- delay(1);
- for(j=0;j<=7;j++)
- {
- PORTE&=~(1<<PE3);
- delay(1);
- if(bit_read(data,j))
- {
- PORTE|=1<<PE4;
- }
- else
- {
- PORTE&=~(1<<PE4);
- }
- delay(1);
- PORTE=1<<PE3;
- delay(1);
- }
- PORTE&=~(1<<PE3);
- delay(1);
- PORTE|=1<<PE5;
- delay(1);
- }
- void WR5954(unsigned char data)
- {
- //PE6-SHCP,PE7-DS,PF0-STCP
- unsigned char j;
- /*SPI发送开始*/
- PORTF&=~(1<<PF0);
- delay(1);
- for(j=0;j<=7;j++)
- {
- PORTE&=~(1<<PE6);
- delay(1);
- if(bit_read(data,j))
- {
- PORTE|=1<<PE7;
- }
- else
- {
- PORTE&=~(1<<PE7);
- }
- delay(1);
- PORTE=1<<PE6;
- delay(1);
- }
- PORTE&=~(1<<PE6);
- delay(1);
- PORTF|=1<<PF0;
- delay(1);
- }
- void WR5955(unsigned char data)
- {
- //PF1-SHCP,PF2-DS,PF3-STCP
- unsigned char j;
- /*SPI发送开始*/
- PORTF&=~(1<<PF3);
- delay(1);
- for(j=0;j<=7;j++)
- {
- PORTF&=~(1<<PF1);
- delay(1);
- if(bit_read(data,j))
- {
- PORTF|=1<<PF2;
- }
- else
- {
- PORTF&=~(1<<PF2);
- }
- delay(1);
- PORTF=1<<PF1;
- delay(1);
- }
- PORTF&=~(1<<PF1);
- delay(1);
- PORTF|=1<<PF3;
- delay(1);
- }
- void WR5956(unsigned char data)
- {
- //PF4-SHCP,PF5-DS,PF6-STCP
- unsigned char j;
- /*SPI发送开始*/
- PORTF&=~(1<<PF6);
- delay(1);
- for(j=0;j<=7;j++)
- {
- PORTF&=~(1<<PF4);
- delay(1);
- if(bit_read(data,j))
- {
- PORTF|=1<<PF5;
- }
- else
- {
- PORTF&=~(1<<PF5);
- }
- delay(1);
- PORTF=1<<PF4;
- delay(1);
- }
- PORTF&=~(1<<PF4);
- delay(1);
- PORTF|=1<<PF6;
- delay(1);
- }
- //
- void main(void)
- {
- init_devices();
- WR5952(0x02);
- //insert your functional code here...
- while(1)
- {
- while(TimerFlg==FALSE); //等待延时标志位
- TimerFlg=FALSE;
- randdata = 2 * rand(); //获得随机数
-
- wdata = randdata/10000; //输出万位
- temp2 = SEGtable[wdata];
- WR5952(temp2);
-
- qdata = randdata%10000/1000; //输出千位
- temp3 = SEGtable[qdata];
- WR5953(temp3);
- baidata = randdata%1000/100; //输出百位
- temp4 = SEGtable[baidata];
- WR5954(temp4);
- sdata = randdata%100/10; //输出10位
- temp5 = SEGtable[sdata];
- WR5955(temp5);
- gdata = randdata%10; //输出个位
- temp6 = SEGtable[gdata];
- WR5956(temp6);
- }
- }
复制代码
所有资料51hei提供下载:
35738618dianzichoujiang.rar
(117.24 KB, 下载次数: 20)
|