1. Functional description
MCU connects 12864 (st7920) LCD and one key to INT0 to simulate a lottery machine.
LCD initial display "Good luck!" ;
Press the button, LCD display "start lottery", and then display "3-2-1" countdown, resulting in 20 (0-4) random numbers;
LCD screen shows "Prize 1", "Prize 2", "Prize 3" and "Thank you for your participation" according to random number.
Finally, the LCD screen shows the result of the lottery: "Congratulations on your winning: XXX".
Press the button again and start the lottery again. The LCD shows "Start the lottery"... Such a cycle.
2. Circuit diagrams designed in PROTEUS
3. Source code
#include <config.h> #include <stdlib.h> #include <12864.h> sbit p=P2^0; void delay_ms(u16 x)//Millisecond delay function { u16 i,j; for(i=0;i<x;i++) for(j=0;j<115;j++); } void main() { u16 m,n; p=0; EA=1; IT0=1; EX0=1; //Initialization lcd12864_init(); lcd12864_pos(1,2); LCD12864disp("Good luck!"); while(1){ if(p==1) { m=rand(); srand(m); lcd12864_clear(); //Count down lcd12864_pos(0,2); LCD12864disp("Start raffle"); lcd12864_pos(2,3); LCD12864disp("3"); delay_ms(1000); lcd12864_clear(); lcd12864_pos(0,2); LCD12864disp("Start raffle"); lcd12864_pos(2,3); LCD12864disp("2"); delay_ms(1000); lcd12864_clear(); lcd12864_pos(0,2); LCD12864disp("Start raffle"); lcd12864_pos(2,3); LCD12864disp("1"); delay_ms(1000); lcd12864_clear(); //Random Generation of 20 Display Awards for(m=0;m<20;m++) { n=rand()%5; lcd12864_pos(2,2); switch(n) { case 0:LCD12864disp("The first prize!");break; case 1:LCD12864disp("1Prize!");break; case 2:LCD12864disp("2Prize!");break; case 3:LCD12864disp("3Prize!");break; case 4:LCD12864disp("Thanks for your participation.");break; } delay_ms(200); } lcd12864_pos(0,1); LCD12864disp("Congratulations on your acquisition:"); } p=0; delay_ms(20); } } void intr0() interrupt 0 // { p=~p; }
4. Experimental results
Because I can't upload videos, so I upload effect videos to Youku, please click and watch - --->. Video of lottery effect