📄 main.c
字号:
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "def.h"
#include "INT.h"
#include "2410lib.h"
#include "spi.h"
#include "lcdlib.h"
#include "touch.h"
#include "CAN.h"
#include "spi.h"
#include "mcp2515.h"
#define WIN_CAN_M 1
#define WIN_CAN_S 2
typedef struct{
U16 X;
U16 Y;
U16 W;
U16 H;
COLOR color;
int mode;
char title[30];
U16 status;
} BUTTON_SUB;
//void DrawFrame(int x0,int y0,int x1,int y1,int Width,COLOR mcolor);
void WIN_CAN(void);
void SetBtn(BUTTON_SUB *Btn, U16 x, U16 y, U16 w, U16 h, char *title, U16 s);
void DrawBtn(BUTTON_SUB bBtn[30], int num);
void DrawCanGuage(int value);
void WIN_CAN_Master(void);
void WIN_CAN_Slave(void);
void __irq TouchPane_Choice(void);
void __irq TouchPane_Can(void);
void __irq Can_LISR(void);
int FindBtnId(int x, int y,BUTTON_SUB bBtn[30],int num);
void CanCmdAnalyse(char Cmd);
void OnBtnCanInc(void);
void OnBtnCanDec(void);
void OnBtnCanRed(void);
void OnBtnCanGreen(void);
void OnBtnCanBlue(void);
int BtnMax;
BUTTON_SUB Btn[20];
int currentWin;
int g_canGuageValue = 0;
volatile int Choice = 0;
int g_redLight = FALSE;
int g_greenLight = FALSE;
int g_blueLight = FALSE;
BUTTON_SUB BtnCan[2];
BUTTON_SUB BtnSubCan[5];
int main(void)
{
ChangeClockDivider(1,1); // 1:2:4
ChangeMPllValue(0xa1,0x3,0x1); // FCLK=202.8MHz
Port_Init();
__vInitHaltHandlers();
LCD_Init();
ISRVector[31] = TouchPane_Choice;
INT_ADC_Enable(TRUE);
ReadAdjustData();
Choice = 0;
WIN_CAN();
while(1)
{
switch(Choice)
{
case 1:
{
WIN_CAN_Master();
g_canGuageValue = 0;
currentWin = WIN_CAN_M;
CAN_Init();
ISRVector[5]=Can_LISR;
while(1);
}break;
case 2:
{
WIN_CAN_Slave();
g_canGuageValue = 0;
currentWin = WIN_CAN_S;
CAN_Init();
ISRVector[5]=Can_LISR;
while(1);
}break;
default:break;
}
}
}
void __irq TouchPane_Choice(void)
{
int XData, YData;
int BtnID;
INT_ADC_Enable(FALSE);
tp_rewaite_int();
XData = adc_x_position();
YData = adc_y_position();
BtnID = FindBtnId(XData,YData,Btn,BtnMax);
switch (BtnID)
{
case 0:
{
Choice = 1;
ISRVector[31] = TouchPane_Can;
Delay(50);
}break;
case 1:
{
Choice = 2;
ISRVector[31] = TouchPane_Can;
Delay(50);
}break;
default :
{
Delay(50);
}
}
ClearInt(); // 清中断
INT_ADC_Enable(TRUE);
}
void __irq TouchPane_Can(void)
{
int XData, YData;
int BtnID;
INT_ADC_Enable(FALSE);
tp_rewaite_int();
XData = adc_x_position();
YData = adc_y_position();
BtnID = FindBtnId(XData,YData,Btn,BtnMax);
switch (BtnID)
{
case 0:
{
OnBtnCanDec();
Delay(50);
}break;
case 1:
{
OnBtnCanInc();
Delay(50);
}break;
case 2:
{
OnBtnCanRed();
Delay(50);
}break;
case 3:
{
OnBtnCanGreen();
Delay(50);
}break;
case 4:
{
OnBtnCanBlue();
Delay(50);
}break;
default :
{
Delay(50);
}
}
ClearInt(); // 清中断
INT_ADC_Enable(TRUE);
}
void __irq Can_LISR(void)
{
int temp;
unsigned char stat, res;
CanMessage msg;
stat = mcp2515_readStatus();
if ( stat & MCP_STAT_RX0IF )
{
// Msg in Buffer 0
mcp2515_read_canMsg( MCP_RXBUF_0,&msg);
mcp2515_modifyRegister(MCP_CANINTF, MCP_RX0IF, 0);
res = CAN_OK;
}
else if ( stat & MCP_STAT_RX1IF )
{
// Msg in Buffer 1
mcp2515_read_canMsg( MCP_RXBUF_1,&msg);
mcp2515_modifyRegister(MCP_CANINTF, MCP_RX1IF, 0);
res = CAN_OK;
}
else
{
res = CAN_NOMSG;
mcp2515_modifyRegister(MCP_CANINTF, 0xff, 0x00);
temp=rEINTPEND;
rEINTPEND=temp;
temp = rSRCPND;
rSRCPND = temp;
temp = rINTPND;
rINTPND = temp;
rGPGDAT|=(1<<3);
return;
}
CanCmdAnalyse(msg.dta[0]);
temp=rEINTPEND;
rEINTPEND=temp;
temp = rSRCPND;
rSRCPND = temp;
temp = rINTPND;
rINTPND = temp;
rGPGDAT|=(1<<3);
}
void WriteCmdForCan(char Cmd)
{
CAN_Write(&Cmd,1);
}
void OnBtnCanInc(void)
{
g_canGuageValue++;
if (g_canGuageValue > 30)
g_canGuageValue = 30;
if (g_canGuageValue < 0)
g_canGuageValue = 0;
DrawCanGuage(g_canGuageValue);
WriteCmdForCan(g_canGuageValue);
Rewriteletter((g_xMax-180)/2+5 ,g_yMax/2-70+23,"数值增加",1,0,RGB(255,0,0),RGB(255,255,255));
}
void OnBtnCanDec(void)
{
g_canGuageValue--;
if (g_canGuageValue > 30)
g_canGuageValue = 30;
if (g_canGuageValue < 0)
g_canGuageValue = 0;
DrawCanGuage(g_canGuageValue);
WriteCmdForCan(g_canGuageValue);
Rewriteletter((g_xMax-180)/2+5 ,g_yMax/2-70+23,"数值减小",1,0,RGB(255,0,0),RGB(255,255,255));
}
void OnBtnCanRed(void)
{
if (g_redLight == TRUE)
{
g_redLight = FALSE;
CanCmdAnalyse(RED_LIGHT_CLOSE);
WriteCmdForCan(RED_LIGHT_CLOSE);
}
else
{
g_redLight = TRUE;
CanCmdAnalyse(RED_LIGHT_OPEN);
WriteCmdForCan(RED_LIGHT_OPEN);
}
}
void OnBtnCanGreen(void)
{
if (g_greenLight == TRUE)
{
g_greenLight = FALSE;
CanCmdAnalyse(GREEN_LIGHT_CLOSE);
WriteCmdForCan(GREEN_LIGHT_CLOSE);
}
else
{
g_greenLight = TRUE;
CanCmdAnalyse(GREEN_LIGHT_OPEN);
WriteCmdForCan(GREEN_LIGHT_OPEN);
}
}
void OnBtnCanBlue(void)
{
if (g_blueLight == TRUE)
{
g_blueLight = FALSE;
CanCmdAnalyse(BLUE_LIGHT_CLOSE);
WriteCmdForCan(BLUE_LIGHT_CLOSE);
}
else
{
g_blueLight = TRUE;
CanCmdAnalyse(BLUE_LIGHT_OPEN);
WriteCmdForCan(BLUE_LIGHT_OPEN);
}
}
void CanCmdAnalyse(char Cmd)
{
char info[20] = {0,};
switch(Cmd)
{
case RED_LIGHT_OPEN:
{
strcpy(info, "红灯点亮");
if (currentWin == WIN_CAN_S)
fillrectangle(g_xMax/2-95, g_yMax/2+20, g_xMax/2-45, g_yMax/2+50, RGB(255,0,0));
}break;
case RED_LIGHT_CLOSE:
{
strcpy(info, "红灯熄灭");
if (currentWin == WIN_CAN_S)
fillrectangle(g_xMax/2-95, g_yMax/2+20, g_xMax/2-45, g_yMax/2+50, RGB(255,255,255));
}break;
case GREEN_LIGHT_OPEN:
{
strcpy(info, "绿灯点亮");
if (currentWin == WIN_CAN_S)
fillrectangle(g_xMax/2-25, g_yMax/2+20, g_xMax/2+25, g_yMax/2+50, RGB(0,255,0));
}break;
case GREEN_LIGHT_CLOSE:
{
strcpy(info, "绿灯熄灭");
if (currentWin == WIN_CAN_S)
fillrectangle(g_xMax/2-25, g_yMax/2+20, g_xMax/2+25, g_yMax/2+50, RGB(255,255,255));
}break;
case BLUE_LIGHT_OPEN:
{
strcpy(info, "蓝灯点亮");
if (currentWin == WIN_CAN_S)
fillrectangle(g_xMax/2+45, g_yMax/2+20, g_xMax/2+95, g_yMax/2+50, RGB(0,0,255));
}break;
case BLUE_LIGHT_CLOSE:
{
strcpy(info, "蓝灯熄灭");
if (currentWin == WIN_CAN_S)
fillrectangle(g_xMax/2+45, g_yMax/2+20, g_xMax/2+95, g_yMax/2+50, RGB(255,255,255));
}break;
default:
{
if (Cmd <= 30)
{
if (Cmd > g_canGuageValue)
strcpy(info, "数值增加");
else
if (Cmd == 0 || Cmd == 30)
strcpy(info, "到达界限");
else
strcpy(info, "数值减小");
g_canGuageValue = Cmd;
DrawCanGuage(Cmd);
}
else
strcpy(info, "无效命令");
}break;
}
Rewriteletter((g_xMax-180)/2+5 ,g_yMax/2-70+23,info,1,0,RGB(255,0,0),RGB(255,255,255));
}
void WIN_CAN(void)
{
SetBtn(&BtnCan[0],(g_xMax-130)/2, g_yMax/2+30, 130,40,"主机",0);
SetBtn(&BtnCan[1],(g_xMax-130)/2, g_yMax/2-30, 130,40,"从机",0);
WIN_WaterTek("CAN总线实验");
DrawBtn(BtnCan, 2);
memcpy (Btn,BtnCan,sizeof(BtnCan));
BtnMax = 2;
}
void SetBtn(BUTTON_SUB *Btn, U16 x, U16 y, U16 w, U16 h, char *title, U16 s)
{
Btn->X = x;
Btn->Y = y;
Btn->W = w;
Btn->H = h;
Btn->status = s;
memcpy(Btn->title, title, strlen(title));
}
void DrawBtn(BUTTON_SUB bBtn[30], int num)
{
int i,xText,yText,textSize;
int BtnColor = RGB(0,0,255);
for(i = 0; i < num; i++)
{
if (bBtn[i].mode == 2)
BtnColor = bBtn[i].color;
else
BtnColor = RGB(0,0,255);;
fillrectangle(bBtn[i].X,
bBtn[i].Y,
bBtn[i].X + bBtn[i].W,
bBtn[i].Y + bBtn[i].H,
BtnColor
);
textSize = strlen(bBtn[i].title) * 8; // 计算名称长度
xText = ((bBtn[i].W - textSize) / 2) + bBtn[i].X; // 计算名称 X 坐标
yText = ((bBtn[i].H - 16) / 2) + 16 + bBtn[i].Y; // 计算名称 Y 坐标
drawletter(xText,yText,bBtn[i].title,1,0,RGB(255,255,0)); // 打印名称
line(bBtn[i].X, bBtn[i].Y + bBtn[i].H, bBtn[i].X + bBtn[i].W, bBtn[i].Y + bBtn[i].H, 0xFFFF);
line(bBtn[i].X, bBtn[i].Y, bBtn[i].X, bBtn[i].Y + bBtn[i].H, 0xFFFF);
line(bBtn[i].X, bBtn[i].Y, bBtn[i].X + bBtn[i].W, bBtn[i].Y, 0x0);
line(bBtn[i].X + bBtn[i].W, bBtn[i].Y, bBtn[i].X + bBtn[i].W, bBtn[i].Y + bBtn[i].H, 0x0);
}
}
void DrawCanGuage(int value)
{
char info[20] = {0,};
int x = (g_xMax-152)/2;
int y = g_yMax/2-10;
int len = 152;
int Max = 30;
int Min = 0;
if (value < Min)
value = Min;
if (value > Max)
value = Max;
DrawFrame(x, y, x+len, y+4, 1, RGB(200,200,200));
fillrectangle(x+1, y+1, x+151, y+3, RGB(255,255,255));
fillrectangle(x+1, y+1, x+1+value*5, y+3, RGB(0,255,64));
line(x+2, y+2, x+value*5, y+2, RGB(0,190,50));
sprintf(info, "%2d", g_canGuageValue);
Rewriteletter((g_xMax)/2-8 ,g_yMax/2+15,info,1,0,RGB(255,255,0),RGB(130,130,240));
}
void WIN_CAN_Master(void)
{
SetBtn(&BtnSubCan[0],(g_xMax-152)/2-21, g_yMax/2-18, 20,20,"-",0);
SetBtn(&BtnSubCan[1],(g_xMax-152)/2+153, g_yMax/2-18, 20,20,"+",0);
SetBtn(&BtnSubCan[2],g_xMax/2-95, g_yMax/2+20, 50,30," ",0);
BtnSubCan[2].color = RGB(255,0,0);
BtnSubCan[2].mode =2;
SetBtn(&BtnSubCan[3],g_xMax/2-25, g_yMax/2+20, 50,30," ",0);
BtnSubCan[3].color = RGB(0,255,0);
BtnSubCan[3].mode =2;
SetBtn(&BtnSubCan[4],g_xMax/2+45, g_yMax/2+20, 50,30," ",0);
BtnSubCan[4].color = RGB(0,0,255);
BtnSubCan[4].mode =2;
WIN_WaterTek("CAN总线实验 - Master");
DrawBtn(BtnSubCan, 5);
drawletter((g_xMax-64)/2 ,g_yMax-40,"主机窗口",1,0,RGB(255,255,0));
fillrectangle((g_xMax-180)/2, g_yMax/2-70, (g_xMax-180)/2+180, g_yMax/2-40, RGB(255,255,255));
DrawFrame((g_xMax-180)/2-2, g_yMax/2-72, (g_xMax-180)/2+182, g_yMax/2-38, 2, RGB(110,120,130));
memcpy(Btn,BtnSubCan,sizeof(BtnSubCan));
BtnMax = 5;
DrawCanGuage(0);
Rewriteletter((g_xMax)/2 ,g_yMax/2+15,"0",1,0,RGB(255,255,0),RGB(130,130,240));
}
void WIN_CAN_Slave(void)
{
SetBtn(&BtnSubCan[0],(g_xMax-152)/2-21, g_yMax/2-18, 20,20,"-",0);
SetBtn(&BtnSubCan[1],(g_xMax-152)/2+153, g_yMax/2-18, 20,20,"+",0);
WIN_WaterTek("CAN总线实验 - Slave");
DrawBtn(BtnSubCan, 2);
drawletter((g_xMax-64)/2 ,g_yMax-40,"从机窗口",1,0,RGB(255,255,0));
fillrectangle((g_xMax-180)/2, g_yMax/2-70, (g_xMax-180)/2+180, g_yMax/2-40, RGB(255,255,255));
DrawFrame((g_xMax-180)/2-2, g_yMax/2-72, (g_xMax-180)/2+182, g_yMax/2-38, 2, RGB(110,120,130));
fillrectangle(g_xMax/2-95, g_yMax/2+20, g_xMax/2-45, g_yMax/2+50, RGB(255,255,255));
DrawFrame(g_xMax/2-97, g_yMax/2+18, g_xMax/2-43, g_yMax/2+52, 2, RGB(110,120,130));
fillrectangle(g_xMax/2-25, g_yMax/2+20, g_xMax/2+25, g_yMax/2+50, RGB(255,255,255));
DrawFrame(g_xMax/2-27, g_yMax/2+18, g_xMax/2+27, g_yMax/2+52, 2, RGB(110,120,130));
fillrectangle(g_xMax/2+45, g_yMax/2+20, g_xMax/2+95, g_yMax/2+50, RGB(255,255,255));
DrawFrame(g_xMax/2+43, g_yMax/2+18, g_xMax/2+97, g_yMax/2+52, 2, RGB(110,120,130));
memcpy (Btn,BtnSubCan,sizeof(BtnSubCan));
BtnMax = 2;
DrawCanGuage(0);
Rewriteletter((g_xMax)/2 ,g_yMax/2+15,"0",1,0,RGB(255,255,0),RGB(130,130,240));
}
int FindBtnId(int x, int y,BUTTON_SUB bBtn[30],int num)
{
int i;
for(i = 0; i < num; i++)
{
if(x>=bBtn[i].X && x<=(bBtn[i].X+bBtn[i].W) &&
y>=(bBtn[i].Y) && y<=(bBtn[i].Y+bBtn[i].H) && (-1 != bBtn[i].status) )
{
fillreverserectangle (bBtn[i].X,bBtn[i].Y,bBtn[i].X+bBtn[i].W,bBtn[i].Y+bBtn[i].H);
WaitForStylusUp();
fillreverserectangle (bBtn[i].X,bBtn[i].Y,bBtn[i].X+bBtn[i].W,bBtn[i].Y+bBtn[i].H);
return i;
}
}
return 0xFF;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -