main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "dac.h"
#include "dht11.h"
#include "dma.h"
#include "stmflash.h"
#include "can.h"
int main(void)
{
u8 key,res,i=0,t=0,cnt=0;
u8 CAN_buf[8];
u8 mode=1;//CAN工作模式:1为环回模式,0为普通模式
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//中断优先级分组2
delay_init(168);
uart_init(115200);//串口波特率115200
LED_init();
LCD_init();
KEY_init();
CAN1_mode_init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,CAN_Mode_LoopBack);//初始化为环回模式,波特率500kbps
while(1)
{
key=KEY_scan(0);
if(key==KEY0_PRES)
{
for(i=0;i<8;i++)
{
CAN_buf[i]=cnt+1;
if(i<4)
LCD_ShowxNum(30+i*32,210,CAN_buf[i],3,16,0x80);//显示数据
else
LCD_ShowxNum(30+(i-4)*32,230,CAN_buf[i],3,16,0x80);
}
res=CAN1_tx_msg(CAN_buf,8);//发送8个字节
if(res)
LCD_ShowString(30+80,190,200,16,16,"Failed");//发送失败
else
LCD_ShowString(30+80,190,200,16,16,"OK ");//发送成功
}
else if(KEY_init==WKUP_PRES)
{
mode=!mode;
CAN1_mode_init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,mode);//初始化为普通模式,波特率500kbps
POINT_COLOR=RED;
if(mode==0)//普通模式,使用两套开发板通信
{
LCD_ShowString(30,130,200,16,16,"Normal Mode");
}
else//环回模式
{
LCD_ShowString(30,130,200,16,16,"LoopBack Mode");
}
}
key=CAN1_rx_msg();
if(key)
{
LCD_Fill(30,270,160,310,WHITE);
for(i=0;i<key;i++)
{
if(i<4)
LCD_ShowxNum(30+i*32,270,CAN_buf[i],3,16,0x80);//显示数据
else
LCD_ShowxNum(30+(i-4)*32,290,CAN_buf[i],3,16,0x80);
}
}
t++;
delay_ms(10);
if(t==20)
{
LED0=!LED0;
t=0;
cnt++;
LCD_ShowxNum(30+48,170,cnt,3,16,0x80);
}
}
}
|