使用中基国威的单片机M307LM写出LCD段码驱动控制代码,使用4个COM口和使用10个SEG口,编写出完整的控制代码
时间: 2024-02-25 20:53:41 浏览: 115
以下是使用中基国威的单片机M307LM驱动LCD的完整代码,使用4个COM口和10个SEG口,实现数字0-9的显示:
```c
#include "M307LM.h"
// 定义COM口和SEG口的引脚
#define COM1 P0_0
#define COM2 P0_1
#define COM3 P0_2
#define COM4 P0_3
#define SEG1 P0_4
#define SEG2 P0_5
#define SEG3 P0_6
#define SEG4 P0_7
#define SEG5 P1_0
#define SEG6 P1_1
#define SEG7 P1_2
#define SEG8 P1_3
#define SEG9 P1_4
#define SEG10 P1_5
// LCD数字段码表
const unsigned char seg_table[] = {
0x3F, 0x06, 0x5B, 0x4F, 0x66,
0x6D, 0x7D, 0x07, 0x7F, 0x67
};
// 延时函数
void delay(unsigned int n)
{
unsigned int i, j;
for(i = 0; i < n; i++)
for(j = 0; j < 1000; j++);
}
// COM口扫描函数
void com_scan(unsigned char com)
{
COM1 = COM2 = COM3 = COM4 = 1;
switch(com)
{
case 1: COM1 = 0; break;
case 2: COM2 = 0; break;
case 3: COM3 = 0; break;
case 4: COM4 = 0; break;
}
}
// SEG口控制函数
void seg_ctrl(unsigned char seg)
{
SEG1 = seg & 0x01;
SEG2 = seg & 0x02;
SEG3 = seg & 0x04;
SEG4 = seg & 0x08;
SEG5 = seg & 0x10;
SEG6 = seg & 0x20;
SEG7 = seg & 0x40;
SEG8 = seg & 0x80;
SEG9 = seg & 0x100;
SEG10 = seg & 0x200;
}
// 显示数字函数
void show_number(unsigned char num)
{
unsigned char seg;
seg = seg_table[num];
for(int i = 1; i <= 4; i++)
{
com_scan(i);
seg_ctrl(seg);
delay(2);
seg_ctrl(0);
}
}
int main(void)
{
while(1)
{
for(int i = 0; i <= 9; i++)
{
show_number(i);
delay(1000);
}
}
}
```
该代码首先定义了COM口和SEG口的引脚,并且定义了LCD数字段码表。然后实现了延时函数、COM口扫描函数和SEG口控制函数。最后实现了显示数字的函数`show_number()`,该函数通过扫描COM口和控制SEG口来显示数字。
在主函数中,实现了循环显示数字0-9的功能,通过调用`show_number()`函数来实现数字的显示。
阅读全文