我正在编写一个 HC6812 微控制器,我需要在 C 中使用 2 个函数来:
关闭端口 B 的 LED
读取 PTH(端口 H)上的开关输入位 0-4
在端口 A 的 7 段显示器上显示开关值。
将端口 B 上的所有 LED 点亮 0-31 秒(由开关设置)。
最后关闭端口 B 的所有 LED。
我的两个功能是Hexled
(读取 Hex 开关输入并在 7 段上显示)和wait
(创建延迟)。
当我编译代码时,我收到 4 个错误:
'pointer expected' 在我的Hexled
函数定义中出现了两次
'函数的参数声明冲突Hexled
'
'重新声明错误Hexled
'
我知道对于 C/C++ 中的函数,我们需要一个函数原型、一个函数定义,并且在主循环中我们调用该函数。我不明白我做错了什么并想从中学习,我尝试研究了许多 C 编程网页。
#include <stdio.h>
void wait(int);
void hexled (unsigned char,unsigned char);
unsigned char switchdata;
unsigned char a;
unsigned char b;
unsigned char mask;
unsigned char index;
unsigned char index1;
/******************* Declare the port addresses **********************/
unsigned char *PORTA = (unsigned char *)0x0000;
unsigned char *DDRA = (unsigned char *)0x0002;
unsigned char *PORTB = (unsigned char *)0x0001;
unsigned char *DDRB = (unsigned char *)0x0003;
unsigned char *PTH = (unsigned char *)0x0260;
unsigned char *DDRH = (unsigned char *)0x0262;
unsigned char *PERH = (unsigned char *)0x0264;
unsigned char LED_data[16] = {0x3F,
0x6,
0x5B,
0x4F,
0x66,
0x6D,
0x7D,
0x7,
0x7F,
0x6F,
0x77,
0x7C,
0x39,
0x5E,
0x79,
0x71}; // LED output
int main(void)
{
/******************* Set up I/O ports********************************/
*DDRH = 0x00; /* make Port H an input port */
*PERH = 0xFF; /* enable Port H */
*DDRA = 0xFF; /* make Port A an output port */
*DDRB = 0xFF; /* make Port B an output port */
/******************* Main loop ***************************************/
*PORTB = 0xFF;
mask = 0b00001111;
switchdata = *PTH & mask;
switchdata = *PORTA;
index = (switchdata & 0b00010000);
index1 = (switchdata | mask);
hexled(index,index1);
*PORTB = 0X00;
wait(31);
*PORTB = 0xFF;
asm ("swi");
return 0;
}
// ***************************HEX FUNCTION**********************************
void hexled(a,b) //* HEXLED function definition
{
if (a)
{
*PORTA = switchdata [b] & 0b00010000;
}
else
{
*PORTA = switchdata [b] | mask;
}
}
// ***************************DELAY FUNCTION************************************
void wait(int seconds) //*WAIT function defintion
{
int x,y,z;
for (x=0; x<seconds; x++)
{
for (y=0; y<=100; y++)
{
for (z=0; z<=2000; z++);
}
}
}