电子抽奖完整项目,基于AVR单片机ATMEG128,包含原理图,源代码及仿真
仿真原理图如下
.
单片机源程序如下:
//ICC-AVR application builder
// Target : M128
// Crystal: 8.0000Mhz
#include #include #include #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< for(j=0;j<=7;j++) { PORTE&=~(1< if(bit_read(data,j)) { PORTE|=1< else { PORTE&=~(1< delay(1); PORTE=1< } PORTE&=~(1< PORTE|=1< } void WR5953(unsigned char data) { //PE3-SHCP,PE4-DS,PE5-STCP unsigned char j; /*SPI发送开始*/ PORTE&=~(1< for(j=0;j<=7;j++) { PORTE&=~(1< if(bit_read(data,j)) { PORTE|=1< else { PORTE&=~(1< delay(1); PORTE=1< } PORTE&=~(1< PORTE|=1< } void WR5954(unsigned char data) { //PE6-SHCP,PE7-DS,PF0-STCP unsigned char j; /*SPI发送开始*/ PORTF&=~(1< for(j=0;j<=7;j++) { PORTE&=~(1< if(bit_read(data,j)) { PORTE|=1< else { PORTE&=~(1< delay(1); PORTE=1< } PORTE&=~(1< PORTF|=1< } void WR5955(unsigned char data) { //PF1-SHCP,PF2-DS,PF3-STCP unsigned char j; /*SPI发送开始*/ PORTF&=~(1< for(j=0;j<=7;j++) { PORTF&=~(1< if(bit_read(data,j)) { PORTF|=1< else { PORTF&=~(1< delay(1); PORTF=1< } PORTF&=~(1< PORTF|=1< } void WR5956(unsigned char data) { //PF4-SHCP,PF5-DS,PF6-STCP unsigned char j; /*SPI发送开始*/ PORTF&=~(1< for(j=0;j<=7;j++) { PORTF&=~(1<
上一篇:ATMEGA48利用按键控制时钟proteus仿真
下一篇:ATMEGA168+L298控制电机正反转的代码与Proteus仿真
推荐阅读最新更新时间:2024-11-08 12:53
设计资源 培训 开发板 精华推荐
- 3W, 1 个 LED 通用 LED 照明驱动器
- 用于开/关控制应用的 AM2G-1215DZ ±15V 2 瓦 DC-DC 转换器的典型应用
- AM6TW-4815SH35Z 15V 6瓦双输出DC-DC转换器的典型应用
- 340g
- AM30EW-2403SZ 3.3V双输出DC/DC转换器典型应用
- LTC2446UHF 演示板,24 位高速 4/8 通道 Delta Sigma ADC
- DC1116A,用于 LTC6103 双通道、高电压、高侧电流检测放大器的演示电路
- EVAL-ADXL335Z,用于 ADXL335 3 轴加速度计的评估板
- usbhub-open
- SP7662 典型应用,12 至 3.3V/12A 同步降压稳压器
- 你评论,我送礼!《玩转TI MSP430 Launchpad》TI社区与EEWORLD联合首发!
- 捉虫行动二:争做EE下载中心黑猫警长!
- 有奖测评 | 英飞凌新品情报站:最新 5V XENSIVTM PAS CO2 传感器 套件测评
- 看视频赢京东卡 | 泰享实测之水哥秘籍第三期
- 喜大普奔,起底USB Tybe-C 五大干货!
- MSP430经典资料大搜集!赢给力TI 无线运动手表开发工具!!!
- 是德科技有奖直播|感恩月开幕式——示波器大讲堂
- 有奖直播|保护嵌入式设备与系统的完整性和可靠性——英飞凌 OPTIGA™ TPM 安全解决方案
- 直播|基于英特尔® Agilex™ FPGA F-Tile的以太网硬核IP详解及如何使用oneAPI对FPGA编程