Home > Article > Backend Development > C language notes - running water ticker based on C language
Today, I suddenly remembered that when I was a sophomore in college, I learned about marquee. At that time, I typed the code one by one and finally ran the marquee state. I still remember that the final effect of running the code on the entity is really pleasant. Without further ado, I will share with you the marquee production process.
1. Title:
##Marquee
(1) Basic requirements
Using 8254 for precise timing, the LED lighting pattern is LED8-LED1. The lighting time interval of each LED is determined by the logic level switches K1, K2 and K3 selection. The time interval of K1 is 0.5S, the time interval of K2 is 1.0S, and the time interval of K3 is 1.5S. Stops the presentation when any key is pressed on the host keyboard.
(2) Design Tips
Use the A port of 8255 to connect the LED light, and the B port to connect the logic level switches K1, K2 and K3 . The control of the light is completed in the interrupt service program of 8254. The main program detects the change of the switch and changes the time interval for lighting the LED light.
(3) Further design requirements
The lighting pattern of LED becomes LED8-LED1-LED8
2. Analysis:
Basic Principle Design
Step 1: 8255 uses the B port to work in mode 0, perform input, and obtain the switch (Note: In order to facilitate the course setting operation, we use K2, K1, K0 replace K3, K2, K1 with the same implementation requirements.) The value of K2K1K0, there will be three values here, which are 100,010,001 in binary encoding, that is, 4, 2, and 1 in decimal, corresponding to the LED light points respectively. The lighting time intervals are 1.5s, 1.0s, and 0.5s.
## Step 2: 8254 adopts working method three , generate a fixed-frequency square wave to continuously trigger the interrupt service routine. In the main program, get the input value of port B of 8255, write the corresponding initial value to counter 1 of 8254 according to the input value, and get a fixed frequency square wave signal at the OUT1 end. Here we connect a K7 corresponding The lamp is used to verify the generation of the square wave and the frequency of the square wave can be roughly judged by the frequency of the lamp flashing, which corresponds to the time intervals of 1.5s, 1.0s, and 0.5s.
##Step 3: Output the square wave signal generated by 8254, Connected to the interrupt interface IR10, it is used to use the frequency of the square wave signal to initiate the interrupt service program at certain time intervals.
Step 4: The interrupt service program is loaded and executed within a certain time interval according to the square wave frequency of 8254. In the interrupt service program, the output of port A of 8255 is changed to control the flashing changes of LED7-LED0, that is, to realize the marquee.
##3. Code implementation:
]
//****************************************************************************************************************** /* 实验接线: 8254: 片选信号CS接Y0;GATE0、GATE1级联接+5V;CLK0接1MHz;CLK1接OUT0; OUT1接k5(进行8254的方波显示); 8255: 片选信号CS接Y1;A口作为输出用排线接LED灯;PB0、PB1、PB2作为输入分别接K0、K1、K2; 中断: IR10接OUT1(中断控制程序控制LED灯的闪烁变化); */ //****************************************************************************************************************** //8255模块的设置 void init(void);//初始化 void ISR(void); int i=0,j=0; const unsigned short Port8255Base = 0x288; const unsigned char ControlWord8255 = 0x82; void init8255(void);// 8255初始化 //****************************************************************************************************************** //关于8254模块的设置 //通道0: 控制字00110110 -> 36H,即0x36,CLK0=1MHz,OUT0=0.01s,初值=F(clk)*T(out) 初始值:COUNT0=10000 //通道1: 控制字01110110 -> 76H,即0x76,CLK1=OUT0,初值=T1(out)/T1(clk) //当OUT1=0.5s,k3=0,k2=0,k0=1,1 初始值:COUNT1=50 //当OUT1=1.0s,k3=0,k2=1,k0=0,2 初始值:COUNT1=100 //当OUT1=1.5s,k3=1,k2=0,k0=0,4 初始值:COUNT1=150 void init8254(void);// 8254初始化 const unsigned short Port8254Base = 0x280, //计数器1端口地址为p8254Base+1,端口地址为p8254Base+3. counter0=10000; const unsigned char ControlWord8254ch0 = 0x36, ControlWord8254ch1 = 0x76; unsigned short counter1=100;//计数器初值 byte pData; unsigned char pdata=0x80; bool flag=true; //****************************************************************************************************************** //主函数 void main() { init(); init8254();// 8254初始化 init8255();//8255初始化 printf("\n start...\n"); while (!_kbhit()) { PortReadByte(Port8255Base + 1, &pData); //写入计数器1的初始值 if(pData==1){ //k0为1的情况,k2k1k0=001,即时间间隔为0.5秒 counter1=50; PortWriteByte(Port8254Base+1 , counter1%256); // 写计数器1计数初值低8位 PortWriteByte(Port8254Base+1 , counter1/256); // 写计数器1计数初值高8位 printf("=switch state:%x LED灯点亮间隔0.5秒=\n",pData); } if(pData==2){ //k1为1的情况,k2k1k0=010,即时间间隔为1.0秒 counter1=100; PortWriteByte(Port8254Base+1 , counter1%256); // 写计数器1计数初值低8位 PortWriteByte(Port8254Base+1 , counter1/256); // 写计数器1计数初值高8位 printf("=switch state:%x LED灯点亮间隔1.0秒=\n",pData); } if(pData==4){ //k2为1的情况,k2k1k0=100,即时间间隔为1.5秒 counter1=150; PortWriteByte(Port8254Base+1 , counter1%256); // 写计数器1计数初值低8位 PortWriteByte(Port8254Base+1 , counter1/256); // 写计数器1计数初值高8位 printf("=switch state:%x LED灯点亮间隔1.5秒=\n",pData); } sleep(3000);// delay 1s EnableIntr(); // 开中断,功能等效于汇编语言指令sti RegisterLocalISREx(ISR,10); // 加载中断服务程 printf("Wait for the switch state change...\n\n"); } _getch(); printf("\n Press any key over...\n"); //DisableIntr(); // 开中断,功能等效于汇编语言指令cli Cleanup(); } //中断服务程序,实现跑马灯的流水 void ISR(void) { if(flag) { _asm { ror pdata,1 //循环右移指令 } } else { _asm { rol pdata,1 //循环左移指令 } } j++; if(j==7) { flag = false; } if(j==14){ flag = true; j=0; } PortWriteByte(Port8255Base,pdata); } //机箱的初始化 void init(void) { if(!Startup()) // 加载实验平台I/O驱动程序 { printf("\n\n ERROR: Open Device Error!请打开实验箱电源\n"); _getch(); exit(0); // return to Windows } } //8254初始化 void init8254(void) { PortWriteByte(Port8254Base + 3, ControlWord8254ch0); // 写计数器0控制字 PortWriteByte(Port8254Base + 3, ControlWord8254ch1); // 写计数器1控制字 PortWriteByte(Port8254Base , counter0%256); // 写计数器0计数初值低8位 PortWriteByte(Port8254Base , counter0/256); // 写计数器0计数初值高8位 } //8255初始化 void init8255(void) { PortWriteByte(Port8255Base + 3, ControlWord8255); // 写8255控制字 PortWriteByte(Port8255Base,pdata); }[Recommended Course: C Video Tutorial
The above is the detailed content of C language notes - running water ticker based on C language. For more information, please follow other related articles on the PHP Chinese website!