遇到了很奇怪的问题,程序明明没错但就是不显示,最后把在main函数中定义的变量count移动到main 外面就正常了 。仿真后发现,在main 函数中定义的局部变量 初始值不为0 ,超出控制范围 , 导致程序跑飞。按理说keil 定义变量默认初始化为0才对 。最后将定义count初始化为0,程序正常运行。下次定义变量一定要初始化啊!!!
在单步调试中,发现这个问题。不得不说一下,keil 在线仿真非常好用。
#include
#include
void spi2Init(void);
void gpioInit(void);
void delay(unsigned int i);
void DisplayOnLattice(unsigned char a,unsigned char b,unsigned char c,unsigned char d );
unsigned char table[]={0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x08,0x00,0x04,0x00,0x02,0x00,0x01,0x00,0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x08,0x00,0x04,0x00,0x02,0x00,0x01};
static unsigned char bb[]={0x01,0x00,0x01,0x00,0x01,0xF0,0x1F,0x80,0x05,0x40,0x09,0x30,0x11,0xCE,0x26,0x40,
0x00,0x80,0x01,0xFC,0x7F,0x00,0x00,0x80,0x00,0x80,0x02,0x80,0x01,0x00,0x00,0x00};
[cpp] view plain copy
//static unsigned char aa[]={0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x08,0x00,0x04,0x00,0x02,0x00,0x01,0x00,0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x08,0x00,0x04,0x00,0x02,0x00,0x01};
static unsigned char cc[]={0x01,0xC0,0x07,0x00,0x01,0xF8,0x1F,0x08,0x21,0x40,0x29,0x00,0x01,0x60,0x0C,0x20,
0x14,0xC0,0x1C,0x60,0x11,0xA0,0x1C,0x40,0x10,0xA0,0x1B,0x18,0x10,0x0E,0x00,0x00};
unsigned char dd[]={0};
unsigned char *pointer[]={bb,cc};
/* 在这里 用到了指针数组pointer ,*(pointer[0])指向数组bb的首个元素地址
*(pointer[1])指向数组cc的首个元素地址
上面字库定义时 bb和cc两个数组是连在一起的,所以下面可以连续滚动多个字
unsigned char *pointer[]={bb,cc};也可以定义成unsigned char *pointer[]={bb};
程序也能正常运行。
若把定义aa数组的注释去掉,则程序将不能显示连续滚动连续的两个汉字的功能:
*/
int main()
{
unsigned char i=0,count=0; //count 没有初始化导致错误
unsigned int time=0;
gpioInit();
spi2Init();
while(1)
{
for(time=0;time<1000;time++) //不行
for(i=0;i<32;i+=2)
DisplayOnLattice(~(*(pointer[0]+i+1+count)),~(*(pointer[0]+i+count)),table[i+1],table[i]);
count+=2;
if(count==17*2)
count=0;
}
}
void DisplayOnLattice(unsigned char a,unsigned char b,unsigned char c,unsigned char d )
{
GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET);
SPI_I2S_SendData(SPI2,a);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==0);
SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_TXE);
SPI_I2S_SendData(SPI2,b);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==0);
SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_TXE);
SPI_I2S_SendData(SPI2,c);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==0);
SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_TXE);
SPI_I2S_SendData(SPI2,d);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==0);
SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_TXE);
delay(1);
GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET);
GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET);
}
void spi2Init()
{
SPI_InitTypeDef SPI_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High ;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; //most significant bit 最高有效位 看具体取模方式
SPI_InitStructure.SPI_CRCPolynomial = 0;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
}
void gpioInit(void)
{
GPIO_InitTypeDef gpio;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
gpio.GPIO_Pin=GPIO_Pin_12; //CS 作为74hc595 RCLK 并行输出 上升沿有效
gpio.GPIO_Speed=GPIO_Speed_50MHz;
gpio.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &gpio);
gpio.GPIO_Pin=GPIO_Pin_13; //SCK 作为74hc595 SRCLK 串行输入时钟
gpio.GPIO_Speed=GPIO_Speed_50MHz;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &gpio);
gpio.GPIO_Pin=GPIO_Pin_14; //MISO 空闲
gpio.GPIO_Speed=GPIO_Speed_50MHz;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &gpio);
gpio.GPIO_Pin=GPIO_Pin_15; //MOSI 作为74hc595 DATA线
gpio.GPIO_Speed=GPIO_Speed_50MHz;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &gpio);
}
void delay(unsigned int i)
{
int j;
for(j=0;j<1000;j++)
for(;i>0; i--);
}
上一篇:STM32事件 中断事件 中断的详解
下一篇:STM32+74HC595:带领你10分钟用对74HC595
推荐阅读最新更新时间:2024-03-16 16:03