|
楼主 |
发表于 2006-12-7 11:23:21
|
显示全部楼层
下面是我一台设备的程序(第二版本,第一台已经用于生产),程序完成量80%
功能:PS/2键盘输入,键F1设置设备的几个参数(需要密码,密码可以更改),键F2设备生产中需要改变的数据,有操作人员修改,其他的就是一些I/O信号的处理
// main
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#include "RTOS_T0.h"
#include "1602LCD.h"
#include "Head.h"
void Task_ScanExPort(void)
{
DDR_CS = 0x3F;
PORT_CS = 0x00;
DisplayString(0,0,9, ucMachineInfo0);
DisplayString(15,1,4, ucMachineInfo1);
delay_ms(500);
LCD_Clean_Screen();
DisplayString(0,0,10, ucMasterInfo0);
DisplayString(10,1,10, ucMasterInfo1);
delay_ms(500);
sbi(PORT_CS, EP_LE);
sbi(ucSystemFlag, SF_BE_LOG);
unsigned char ucExPort_Temp;
unsigned char ucExPort_Index;
unsigned char ucExPort_PortValue;
TaskStart:
for(ucExPort_Index = 0; ucExPort_Index < 8; ucExPort_Index++)
{
PORT_CS &= 0xF0;
PORT_CS |= ucExPort_Index;
if(ucExPort_Index >= 4)
{
DDR_EP = 0xFF;
PORT_EP = ucEP_OUT[ucExPort_Index-4];
}
sbi(PORT_CS, CS_OE);
asm("nop"); // can not be ignore
if(ucExPort_Index < 4)
{
DDR_EP = 0x00;
PORT_EP = 0xFF;
asm("nop");
ucExPort_PortValue = ~PIN_EP;
ucExPort_Temp = ucExPort_PortValue ^ ucEP_IN[ucExPort_Index].ucPV;
ucEP_IN[ucExPort_Index].ucBD = ucExPort_Temp & ucEP_IN[ucExPort_Index].ucPV;
ucEP_IN[ucExPort_Index].ucBA = ucExPort_Temp & ucExPort_PortValue;
ucEP_IN[ucExPort_Index].ucPV = ucExPort_PortValue;
}
}
OS_CallNext();
goto TaskStart;
}
void Task_Keyboard(void)
{
loop_until_bit_is_set(ucSystemFlag, SF_BE_LOG);
DDRD &= 0xF3;
PORTD |= 0x0C;
MCUCR = (1<<ISC01); // INT0 下降沿触发
GICR |= (1<<INT0);
ucKeyPulse = 10;
TaskStart:
if( ucKeyCode == F2 ) // Menu Select
{
if( ucCurrentMenu == MENU_PROD ) // Prod
{
sbi(ucSystemFlag, SF_BE_SET);
delay_ms(10);
LCD_Clean_Screen();
DisplayString(0,0,12, ucProdMenu);
DisplayString(0,1,5, ucSetFlag);
LCD_DisplayNum(16,1, uiProdQty);
ucCurrentMenu = MENU_TAIL;
MenuUpdate(MENU_PROD);
}
else if( ucCurrentMenu == MENU_TAIL ) // Tail
{
LCD_Clean_Screen();
DisplayString(0,0,9, ucTailMenu);
DisplayString(0,1,5, ucSetFlag);
LCD_DisplayNum(16,1, uiTailQty);
ucCurrentMenu = MENU_LEAD;
MenuUpdate(MENU_TAIL);
}
else if( ucCurrentMenu == MENU_LEAD ) // Lead
{
LCD_Clean_Screen();
DisplayString(0,0,9, ucLeadMenu);
DisplayString(0,1,5, ucSetFlag);
LCD_DisplayNum(16,1, uiLeadQty);
ucCurrentMenu = MENU_USET;
MenuUpdate(MENU_LEAD);
}
else if( ucCurrentMenu == MENU_USET ) // Return
{
ucKeyCode = 0;
ucCurrentMenu = 0;
PrimalScreen();
cbi(ucSystemFlag, SF_BE_SET);
}
}
else if(ucKeyCode == F1)
{
if( ucCurrentMenu == MENU_PW ) // Password
{
sbi(ucSystemFlag, SF_BE_SET);
delay_ms(10);
LCD_Clean_Screen();
DisplayString(0,0,9, ucPassword);
DisplayString(0,1,5, ucSetFlag);
DisplayString(16,1,4, ucPWS);
ucCurrentMenu = MENU_HD;
SetParameters(MENU_PW);
}
else if( ucCurrentMenu == MENU_HD ) // Heat delay time
{
LCD_Clean_Screen();
DisplayString(0,0,14, ucHeat);
DisplayString(0,1,5, ucSetFlag);
LCD_DisplayNum(16,1, uiHeatDly);
ucCurrentMenu = MENU_LD;
SetParameters(MENU_HD);
}
else if( ucCurrentMenu == MENU_LD ) // load delay time
{
LCD_Clean_Screen();
DisplayString(0,0,14, ucLoad);
DisplayString(0,1,5, ucSetFlag);
LCD_DisplayNum(16,1, uiLoadDly);
ucCurrentMenu = MENU_UD;
SetParameters(MENU_LD);
}
else if( ucCurrentMenu == MENU_UD ) // unload delay time
{
LCD_Clean_Screen();
DisplayString(0,0,15, ucUnload);
DisplayString(0,1,5, ucSetFlag);
LCD_DisplayNum(16,1, uiUnloadDly);
ucCurrentMenu = MENU_NP;
SetParameters(MENU_UD);
}
else if( ucCurrentMenu == MENU_NP ) // New password
{
LCD_Clean_Screen();
DisplayString(0,0,13, ucNewPWD);
DisplayString(0,1,5, ucSetFlag);
DisplayString(16,1,4, ucPWS);
ucCurrentMenu = MENU_USET;
SetParameters(MENU_NP);
}
else if( ucCurrentMenu == MENU_USET ) // Return
{
ucKeyCode = 0;
ucCurrentMenu = 0;
PrimalScreen();
cbi(ucSystemFlag, SF_BE_SET);
}
}
else if(ucKeyCode == F3 && ucErrorCode == 0)
{
if( bit_is_clear(ucSystemFlag, SF_BE_LEAD) )
{
ucKeyCode = 0;
sbi(ucSystemFlag, SF_BE_LEAD);
delay_ms(10);
ShowLead();
while(true)
{
DisplayString(16,1,4, ucSpace);
LCD_DisplayNum(16,1, uiLeadResidual);
delay_ms(2500);
if(ucKeyCode == ESC || uiLeadResidual == 0)
{
ucKeyCode = 0;
uiLeadResidual = uiLeadQty;
break;
}
if(ucErrorCode) break;
uiLeadResidual -= 3;
}
cbi(ucSystemFlag, SF_BE_LEAD);
PrimalScreen();
delay_ms(10);
cbi(ucSystemFlag, SF_BE_ERR);
}
}
OS_CallNext();
goto TaskStart;
}
void Task_System(void)
{
loop_until_bit_is_set(ucSystemFlag, SF_BE_LOG);
TaskStart:
if( bit_is_set(ucEP_IN[0].ucPV, IN_UNLOAD) ) sbi(ucEP_OUT[3], OUT_ENABLE_UNLOAD_MOTOR);
if( bit_is_set(ucEP_IN[0].ucPV, IN_LOAD) ) sbi(ucEP_OUT[3], OUT_ENABLE_LOAD_MOTOR);
OS_CallNext();
goto TaskStart;
}
void Task_Load(void)
{
loop_until_bit_is_set(ucSystemFlag, SF_BE_LOG);
TaskStart:
if( bit_is_clear(ucEP_IN[0].ucPV, IN_LOAD) )
{
if( bit_is_set(ucEP_OUT[3], OUT_ENABLE_LOAD_MOTOR) )
{
delay_ms(uiLoadDly);
cbi(ucEP_OUT[3], OUT_ENABLE_LOAD_MOTOR);
}
}
OS_CallNext();
goto TaskStart;
}
void Task_Unload(void)
{
loop_until_bit_is_set(ucSystemFlag, SF_BE_LOG);
TaskStart:
if( bit_is_clear(ucEP_IN[0].ucPV, IN_UNLOAD) )
{
if( bit_is_set(ucEP_OUT[3], OUT_ENABLE_UNLOAD_MOTOR) )
{
delay_ms(uiUnloadDly);
cbi(ucEP_OUT[3], OUT_ENABLE_UNLOAD_MOTOR);
}
}
OS_CallNext();
goto TaskStart;
}
void Task_SystemStatus(void)
{
loop_until_bit_is_set(ucSystemFlag, SF_BE_LOG);
unsigned char ucIndex;
TaskStart:
if( bit_is_clear(ucEP_IN[1].ucPV, IN_SVO_IS_ON) ) sbi(ucErrorCode, EC_SVO); else {cbi(ucErrorCode, EC_SVO);cbi(ucErrorFlag, EC_SVO);}
if( bit_is_clear(ucEP_IN[1].ucPV, IN_HOST_READY) ) sbi(ucErrorCode, EC_HRD); else {cbi(ucErrorCode, EC_HRD);cbi(ucErrorFlag, EC_HRD);}
if( bit_is_set(ucEP_IN[0].ucPV, IN_NOT_AT_T_ALARM) ) sbi(ucErrorCode, EC_NAT); else {cbi(ucErrorCode, EC_NAT);cbi(ucErrorFlag, EC_NAT);}
if( bit_is_set(ucEP_IN[0].ucPV, IN_LOW_COVER_TAPE_ALARM) ) sbi(ucErrorCode, EC_LCT); else {cbi(ucErrorCode, EC_LCT);cbi(ucErrorFlag, EC_LCT);}
if( bit_is_set(ucEP_IN[0].ucPV, IN_PART_UP_IN_TAPE_ALARM) ) sbi(ucErrorCode, EC_PIT); else {cbi(ucErrorCode, EC_PIT);cbi(ucErrorFlag, EC_PIT);}
if( ucErrorCode )
{
if( bit_is_set(ucErrorCode, EC_MIS) ) // Missing
{
ucEP_OUT[2] &= 0xF8;
ucEP_OUT[2] |= 0x01;
}
else if( bit_is_set(ucErrorCode, EC_EXT) ) // Extra
{
ucEP_OUT[2] &= 0xF8;
ucEP_OUT[2] |= 0x02;
}
else if( bit_is_set(ucErrorCode, EC_PIT) ) // Part up
{
ucEP_OUT[2] &= 0xF8;
ucEP_OUT[2] |= 0x03;
}
else if( bit_is_set(ucErrorCode, EC_LCT) ) // Low cover tape
{
ucEP_OUT[2] &= 0xF8;
ucEP_OUT[2] |= 0x06;
}
else if( bit_is_set(ucErrorCode, EC_NAT) ) // Not at temperature
{
ucEP_OUT[2] &= 0xF8;
ucEP_OUT[2] |= 0x04;
}
else
{
ucEP_OUT[2] &= 0xF8;
}
for(ucIndex=0; ucIndex<7; ucIndex++)
{
if( bit_is_set(ucErrorCode, ucIndex) )
{
ShowError(ucIndex);
break;
}
}
for(++ucIndex; ucIndex<7; ucIndex++)
{
cbi(ucErrorFlag, ucIndex);
}
eoi(ucEP_OUT[3], OUT_SYSTEM_LED);
delay_ms(250);
cbi(ucSystemFlag, SF_BE_ERR);
}
else
{
sbi(ucEP_OUT[3], OUT_SYSTEM_LED);
if( bit_is_clear(ucSystemFlag, SF_BE_ERR) && bit_is_clear(ucSystemFlag, SF_BE_SET) )
{
PrimalScreen();
sbi(ucSystemFlag, SF_BE_ERR);
}
if( bit_is_clear(ucSystemFlag, SF_BE_SET) )
{
if( bit_is_clear(ucSystemFlag, SF_BE_LEAD) && bit_is_clear(ucSystemFlag, SF_BE_TAIL) ) ShowProdResidual();
}
}
OS_CallNext();
goto TaskStart;
}
void Task_CamDisc(void)
{
loop_until_bit_is_set(ucSystemFlag, SF_BE_LOG);
TaskStart:
// Test code
if( bit_is_set(ucEP_IN[3].ucBD, 7) )
{
uiProdResidual -= 3;
if(uiProdResidual == 0) uiProdResidual = uiProdQty;
}
OS_CallNext();
goto TaskStart;
}
void Task_Miss_Extra(void)
{
loop_until_bit_is_set(ucSystemFlag, SF_BE_LOG);
TaskStart:
// Test code
eoi(ucEP_OUT[1],0);
delay_ms(100);
OS_CallNext();
goto TaskStart;
}
int main(void)
{
LCD_Init();
uiHeatDly = eeprom_read_word(0);
uiLoadDly = eeprom_read_word(2);
uiUnloadDly = eeprom_read_word(4);
uiProdQty = uiProdResidual = eeprom_read_word(6);
uiTailQty = uiTailResidual = eeprom_read_word(8);
uiLeadQty = uiLeadResidual = eeprom_read_word(10);
ucPWD[0] = eeprom_read_byte(12);
ucPWD[1] = eeprom_read_byte(13);
ucPWD[2] = eeprom_read_byte(14);
ucPWD[3] = eeprom_read_byte(15);
if(ucPWD[0] == 0xFF) ucPWD[0] = '1';
if(ucPWD[1] == 0xFF) ucPWD[1] = '2';
if(ucPWD[2] == 0xFF) ucPWD[2] = '3';
if(ucPWD[3] == 0xFF) ucPWD[3] = '4';
uTotalTaskNum = 0;
uCurrentTaskID = 0;
OS_Timer0Init();
OS_CreateTask(Task_ScanExPort, &uTaskStack[Total_Stack_Size-1]);
OS_CreateTask(Task_Keyboard, &uTaskStack[Total_Stack_Size-Task_Stack_Size-1]);
OS_CreateTask(Task_System, &uTaskStack[Total_Stack_Size-Task_Stack_Size*2-1]);
OS_CreateTask(Task_Load, &uTaskStack[Total_Stack_Size-Task_Stack_Size*3-1]);
OS_CreateTask(Task_Unload, &uTaskStack[Total_Stack_Size-Task_Stack_Size*4-1]);
OS_CreateTask(Task_SystemStatus, &uTaskStack[Total_Stack_Size-Task_Stack_Size*5-1]);
OS_CreateTask(Task_CamDisc, &uTaskStack[Total_Stack_Size-Task_Stack_Size*6-1]);
OS_CreateTask(Task_Miss_Extra, &uTaskStack[Total_Stack_Size-Task_Stack_Size*7-1]);
OS_Start();
return(0);
}
// Head.h
#ifndef _HEAD_H
#define _HEAD_H
#define DDR_CS DDRA
#define PORT_CS PORTA
#define PIN_CS PINA
#define DDR_EP DDRB
#define PORT_EP PORTB
#define PIN_EP PINB
#define CS_OE 3
#define EP_LE 4
// PORTA
// 6,7: NC
// 5: A/B
// 4: OUT_E
// 3: OE1 138
// A2-A0 addr
volatile struct PORT_EX_I
{
unsigned char ucPV;
unsigned char ucBA; // Ascend
unsigned char ucBD; // Descend
}
ucEP_IN[4];
volatile unsigned char ucEP_OUT[4];
const unsigned char ucSetFlag[5] PROGMEM = "[SET]";
const unsigned char ucErrFlag[5] PROGMEM = "[ERR]";
const unsigned char ucSucFlag[5] PROGMEM = "[SUC]";
const unsigned char ucMasterInfo0[10] PROGMEM = "Rising Sun";
const unsigned char ucMasterInfo1[10] PROGMEM = "Automation";
const unsigned char ucMachineInfo0[9] PROGMEM = "TAPE REEL";
const unsigned char ucMachineInfo1[4] PROGMEM = "V1.1";
const unsigned char ucProdMenu[12] PROGMEM = "Product QTY:";
const unsigned char ucTailMenu[9] PROGMEM = "Tail QTY:";
const unsigned char ucLeadMenu[9] PROGMEM = "Lead QTY:";
const unsigned char ucPWS[4] PROGMEM = "____";
const unsigned char ucPassword[9] PROGMEM = "PASSWORD:";
const unsigned char ucHeat[14] PROGMEM = "HEATER DELAY:";
const unsigned char ucLoad[14] PROGMEM = "LOADER DELAY:";
const unsigned char ucUnload[15] PROGMEM = "UNLOADER DELAY:";
const unsigned char ucNewPWD[13] PROGMEM = "NEW PASSWORD:";
const unsigned char ucErr[6] PROGMEM = "ERROR:";
const unsigned char ucErr_Code[][18] PROGMEM = {"SERVO OFF","HOST IS NOT READY","NOT AT TEMPERATURE","LOW COVER TAPE","PART UP IN TAPE","EXTRA PART","MISSING PART"};
const unsigned char ucErr_Leng[7] = {9,17,18,14,15,10,12};
const unsigned char ucSpace[20] PROGMEM = " ";
unsigned char ucStrTemp[20];
volatile unsigned char ucPWD[4];
volatile unsigned int uiHeatDly;
volatile unsigned int uiLoadDly;
volatile unsigned int uiUnloadDly;
volatile unsigned int uiProdQty;
volatile unsigned int uiProdResidual;
volatile unsigned int uiProdTemp;
volatile unsigned int uiTailQty;
volatile unsigned int uiTailResidual;
volatile unsigned int uiLeadQty;
volatile unsigned int uiLeadResidual;
volatile unsigned char ucKeyPulse;
volatile unsigned char ucKeyCode;
volatile unsigned char ucKeyFlag;
volatile unsigned char ucKeyTemp;
volatile unsigned char ucErrorFlag;
#define EF_BE_SVO 0
#define EF_BE_HRD 1
#define EF_BE_NAT 2
#define EF_BE_LCT 3
#define EF_BE_PIT 4
#define EF_BE_EXT 5
#define EF_BE_MIS 6
// 0: is svo on ?
// 1: is host ready ?
// 2: is not at temperatue ?
// 3: is low cover tape ?
// 4: is part up in tape ?
// 5: is extra part in tape ?
// 6: is missing part in tape
volatile unsigned char ucErrorCode;
#define EC_SVO 0
#define EC_HRD 1
#define EC_NAT 2
#define EC_LCT 3
#define EC_PIT 4
#define EC_EXT 5
#define EC_MIS 6
// 0: is servo on ?
// 1: is host ready ?
// 2: is not at temperature ?
// 3: is low cover tape ?
// 4: is part up in tape ?
// 5: is extra part in tape ?
// 6: is missing part in tape
volatile unsigned char ucSystemFlag;
#define SF_BE_LOG 0
#define SF_BE_SET 1
#define SF_BE_ERR 2
#define SF_BE_PROD 3
#define SF_BE_TAIL 4
#define SF_BE_LEAD 5
// 0: is finished to login ?
// 1: is in setting ?
// 2: is an error in system ?
// 3: is run porduct ?
// 4: is run tail ?
// 5: is run lead ?
volatile unsigned char ucCurrentMenu;
#define MENU_PROD 0
#define MENU_TAIL 1
#define MENU_LEAD 2
#define MENU_USET 0xFF
#define MENU_PW 0
#define MENU_HD 1
#define MENU_LD 2
#define MENU_UD 3
#define MENU_NP 4
#define true 1
#define false 0
#define KB_CLK 2
#define KB_DAT 3
#define F1 0xF1 //0x05
#define F2 0x06
#define F3 0x04
#define F4 0x0C
#define F5 0x03
#define F6 0x0B
#define N0 0x30 //0x70
#define N1 0x31 //0x69
#define N2 0x32 //0x72
#define N3 0x33 //0x7A
#define N4 0x34 //0x6B
#define N5 0x35 //0x73
#define N6 0x36 //0x74
#define N7 0x37 //0x6C
#define N8 0x38 //0x75
#define N9 0x39 //0x7D
#define ESC 0x76
#define ENTER 0x5A
#define POINT 0x71
#define CLEAR 0x66
// Input Channel 0
#define IN_LOAD 0 // 上料传感器 OMRON EE-SX670A
#define IN_UNLOAD 1 // 下料传感器 OMRON EE-SX670A
#define IN_CAM_HIGH 2 // 凸轮高位 OMRON EE-SX670A
#define IN_CAM_LOW 3 // 凸轮低位 OMRON EE-SX670A
#define IN_NOT_AT_T_ALARM 4 // 稳控器报警 OMRON E5GN
#define IN_LOW_COVER_TAPE_ALARM 5 // 光纤传感器 KEYENCE FS-V12
#define IN_MISS_EXTRA_PART_ALARM 6 // 光纤传感器 KEYENCE FS-V12
#define IN_PART_UP_IN_TAPE_ALARM 7 // 光纤传感器 KEYENCE FS-V12
// Input Channel 1
#define IN_HOST_READY 0 // 主机就绪
#define IN_HOST_NEXT_STEP 1 // 主机通知步进一步
#define IN_HOST_RESET_SIGNAL 2 // 主机复位信号
#define IN_SVO_IS_READY 3 // SVO 就绪
#define IN_SVO_IS_ON 4 // SVO ON
#define IN_SVO_IS_TPOS 5 // SVO 目标位置到达
#define IN_SVO_IS_ALARM 6 // SVO 报警
// Output Channel 3
#define OUT_ENABLE_LOAD_MOTOR 7 // 上料电机使能
#define OUT_ENABLE_UNLOAD_MOTOR 6 // 下料电机使能
#define OUT_ENABLE_CAM_MOTOR 5 // 凸轮电机使能
#define OUT_SYSTEM_LED 4 // 系统状态指示灯
#define OUT_ELEC_VALVE_PUSH 3 // 汽缸电磁阀下推
#define OUT_ELEC_VALVE_PULL 2 // 汽缸电磁阀上拉
#define OUT_SVO_CTRG 1 // SVO 步进触发
#define OUT_SVO_RESET 0 // SVO 错误复位
// Output Channel 2
#define OUT_HOST_WAIT_FOR_TAIL 5 // HOST 701
#define OUT_HOST_WAIT_FOR_NEXT_PART 4 // HOST 702
#define OUT_HOST_WAIT_FOR_NEXT_LOT 3 // HOST 703
#define OUT_HOST_ALARM_BIT6 2 // HOST 704
#define OUT_HOST_ALARM_BIT5 1 // HOST 705
#define OUT_HOST_ALARM_BIT4 0 // HOST 706
// 4 5 6
// 1 0 0 1 : Missing part in Tape
// 2 0 1 0 : Extra part in tape
// 3 0 1 1 : Part up in tape
// 4 1 0 0 : Not at temperature
// 5 1 0 1 : Tape Index Failure
// 6 1 1 0 : Low cover tape
void DisplayString(unsigned char X, unsigned char Y, unsigned char ucLen, const prog_uchar* ucString)
{
unsigned char n;
for(n=0;n<20;n++) ucStrTemp[n] = 0;
memcpy_P(ucStrTemp, ucString, ucLen);
LCD_DisplayString(X,Y, ucStrTemp);
}
void ShowProd(void)
{
LCD_Clean_Screen();
DisplayString(0,0,12, ucProdMenu);
LCD_DisplayNum(0,1, uiProdQty);
LCD_DisplayNum(16,1, uiProdResidual);
}
void ShowTail(void)
{
LCD_Clean_Screen();
DisplayString(0,0,9, ucTailMenu);
LCD_DisplayNum(0,1, uiTailQty);
LCD_DisplayNum(16,1, uiTailResidual);
}
void ShowLead(void)
{
LCD_Clean_Screen();
DisplayString(0,0,9, ucLeadMenu);
LCD_DisplayNum(0,1, uiLeadQty);
LCD_DisplayNum(16,1, uiLeadResidual);
}
void ShowProdResidual(void)
{
if(uiProdTemp != uiProdResidual)
{
DisplayString(16,1,4, ucSpace);
LCD_DisplayNum(16,1, uiProdResidual);
uiProdTemp = uiProdResidual;
}
}
void PrimalScreen(void)
{
if(ucErrorCode)
{
ucErrorFlag = 0; // reset error flag that show error
}
else
{
if( bit_is_clear(ucSystemFlag, SF_BE_LEAD) && bit_is_clear(ucSystemFlag, SF_BE_TAIL) ) ShowProd();
}
}
void EscMenu(void)
{
ucCurrentMenu = 0;
PrimalScreen();
cbi(ucSystemFlag, SF_BE_SET);
}
void SetERR(void)
{
DisplayString(0,1,5, ucErrFlag);
delay_ms(500);
DisplayString(0,1,5, ucSetFlag);
}
void SetSUC(void)
{
DisplayString(0,1,5, ucSucFlag);
delay_ms(500);
}
void SetParameters(unsigned char ucParaIndex)
{
unsigned char ucPos = 0;
unsigned int ucTemp = 0;
unsigned char ucIndex = 0;
unsigned char ucInput[4];
while(true)
{
ucKeyCode = 0;
while(!ucKeyCode); // wait_until_key_is_pressed
if(ucKeyCode >= N0 && ucKeyCode <= N9 && ucPos < 4)
{
if(ucParaIndex == MENU_PW || ucParaIndex == MENU_NP)
{
if(ucKeyCode >= N0 && ucKeyCode <= N9 && ucPos < 4)
{
LCD_Set_XY(16+ucPos, 1);
LCD_Send_Command('*', 1);
ucInput[ucPos++] = ucKeyCode;
}
}
else
{
if(ucPos)
{
LCD_Set_XY(16+ucPos, 1);
LCD_Send_Command(ucKeyCode, 1);
ucInput[ucPos++] = ucKeyCode-0x30;
}
else if(!ucPos && ucKeyCode != N0)
{
DisplayString(16,1,4, ucSpace);
LCD_Set_XY(16+ucPos, 1);
LCD_Send_Command(ucKeyCode, 1);
ucInput[ucPos++] = ucKeyCode-0x30;
}
}
}
else if(ucKeyCode == ENTER)
{
if(ucParaIndex == MENU_PW)
{
for(ucIndex = 0; ucIndex < ucPos; ucIndex++)
{
if(ucPWD[ucIndex] != ucInput[ucIndex])
{
SetERR();
DisplayString(16,1,4, ucPWS);
break;
}
ucInput[ucIndex] = 0;
}
ucPos = 0;
if(ucIndex == 4)
{
SetSUC();
ucKeyCode = F1;
break;
}
}
else if(ucParaIndex == MENU_NP)
{
if(ucPos < 4)
{
SetERR();
DisplayString(16,1,4, ucPWS);
ucPos = 0;
}
else
{
eeprom_write_byte(12, ucInput[0]);
eeprom_write_byte(13, ucInput[1]);
eeprom_write_byte(14, ucInput[2]);
eeprom_write_byte(15, ucInput[3]);
ucPWD[0] = ucInput[0];
ucPWD[1] = ucInput[1];
ucPWD[2] = ucInput[2];
ucPWD[3] = ucInput[3];
SetSUC();
ucKeyCode = F1;
break;
}
for(ucIndex = 0; ucIndex < 4; ucIndex++) ucInput[ucIndex] = 0;
}
else
{
ucTemp = 0;
for(ucIndex = 0; ucIndex < ucPos; ucIndex++)
{
ucTemp = ucTemp*10 + ucInput[ucIndex];
ucInput[ucIndex] = 0;
}
if( ucTemp < 50)
{
SetERR();
if(ucParaIndex == MENU_HD) LCD_DisplayNum(16,1, uiHeatDly);
else if(ucParaIndex == MENU_LD) LCD_DisplayNum(16,1, uiLoadDly);
else if(ucParaIndex == MENU_UD) LCD_DisplayNum(16,1, uiUnloadDly);
ucPos = 0;
ucTemp = 0;
}
else
{
if(ucParaIndex == MENU_HD)
{
uiHeatDly = ucTemp;
eeprom_write_word(0, ucTemp);
}
else if(ucParaIndex == MENU_LD)
{
uiLoadDly = ucTemp;
eeprom_write_word(2, ucTemp);
}
else if(ucParaIndex == MENU_UD)
{
uiUnloadDly = ucTemp;
eeprom_write_word(4, ucTemp);
}
SetSUC();
ucKeyCode = F1;
break;
}
}
}
else if(ucKeyCode == ESC)
{
delay_ms(10);
EscMenu();
break;
}
else if(ucKeyCode == CLEAR)
{
if(ucParaIndex == MENU_PW || ucParaIndex == MENU_NP)
{
if(ucPos)
{
--ucPos;
LCD_Set_XY(16+ucPos, 1);
LCD_Send_Command('_', 1);
ucInput[ucPos] = 0;
}
}
else
{
DisplayString(16,1,4, ucSpace);
ucTemp = 0;
for(ucIndex = 0; ucIndex < ucPos-1; ucIndex++)
{
ucTemp = ucTemp*10 + ucInput[ucIndex];
}
if(ucPos) ucPos--;
ucInput[ucPos] = 0;
LCD_DisplayNum(16,1, ucTemp);
}
}
else if(ucKeyCode == F1 && ucPos == 0 && ucParaIndex != MENU_PW)
{
ucKeyCode = F1;
break;
}
}
}
void MenuUpdate(unsigned char ucMenuIndex)
{
unsigned char ucPos = 0;
unsigned int ucTemp = 0;
unsigned char ucIndex = 0;
unsigned char ucInput[4];
while(true)
{
ucKeyCode = 0;
while(!ucKeyCode); // wait_until_key_is_pressed
if(ucKeyCode >= N0 && ucKeyCode <= N9 && ucPos < 4)
{
if(ucPos)
{
LCD_Set_XY(16+ucPos, 1);
LCD_Send_Command(ucKeyCode, 1);
ucInput[ucPos++] = ucKeyCode-0x30;
}
else
if(!ucPos && ucKeyCode != N0)
{
DisplayString(16,1,4, ucSpace);
LCD_Set_XY(16+ucPos, 1);
LCD_Send_Command(ucKeyCode, 1);
ucInput[ucPos++] = ucKeyCode-0x30;
}
}
else if(ucKeyCode == ENTER)
{
ucTemp = 0;
for(ucIndex = 0; ucIndex < ucPos; ucIndex++)
{
ucTemp = ucTemp*10 + ucInput[ucIndex];
ucInput[ucIndex] = 0;
}
if( ucTemp%3 || ucTemp==0)
{
SetERR();
if(ucMenuIndex == MENU_PROD) LCD_DisplayNum(16,1, uiProdQty);
else if(ucMenuIndex == MENU_TAIL) LCD_DisplayNum(16,1, uiTailQty);
else if(ucMenuIndex == MENU_LEAD) LCD_DisplayNum(16,1, uiLeadQty);
ucPos = 0;
ucTemp = 0;
}
else
{
if(ucMenuIndex == MENU_PROD)
{
uiProdQty = uiProdResidual = ucTemp;
eeprom_write_word(6, ucTemp);
}
else if(ucMenuIndex == MENU_TAIL)
{
uiTailQty = uiTailResidual = ucTemp;
eeprom_write_word(8, ucTemp);
}
else if(ucMenuIndex == MENU_LEAD)
{
uiLeadQty = uiLeadResidual = ucTemp;
eeprom_write_word(10, ucTemp);
}
SetSUC();
ucKeyCode = F2;
break;
}
}
else if(ucKeyCode == ESC)
{
delay_ms(10);
EscMenu();
break;
}
else if(ucKeyCode == CLEAR)
{
DisplayString(16,1,4, ucSpace);
ucTemp = 0;
for(ucIndex = 0; ucIndex < ucPos-1; ucIndex++)
{
ucTemp = ucTemp*10 + ucInput[ucIndex];
}
if(ucPos) ucPos--;
ucInput[ucPos] = 0;
LCD_DisplayNum(16,1, ucTemp);
}
else if(ucKeyCode == F2 && ucPos == 0)
{
ucKeyCode = F2;
break;
}
}
}
void ShowError(unsigned char ucCode)
{
if( bit_is_clear(ucErrorFlag, ucCode) && bit_is_clear(ucSystemFlag, SF_BE_SET) )
{
LCD_Clean_Screen();
DisplayString(0,0,6, ucErr);
DisplayString(20-ucErr_Leng[ucCode],1,ucErr_Leng[ucCode], ucErr_Code[ucCode]);
sbi(ucErrorFlag, ucCode);
}
}
SIGNAL(SIG_INTERRUPT0)
{
if( ucKeyPulse < 8 ) // Data
{
if( bit_is_set(PIND, KB_DAT) ) sbi(ucKeyTemp, ucKeyPulse);
else cbi(ucKeyTemp, ucKeyPulse);
++ucKeyPulse;
}
else
if( ucKeyPulse == 8) // Parity
{
++ucKeyPulse;
}
else
if( ucKeyPulse == 9) // Stopbit
{
if( bit_is_set(PIND, KB_DAT) )
{
if( ucKeyTemp == 0xE0 )
{
ucKeyFlag = 1;
}
else if( ucKeyTemp == 0xF0 )
{
ucKeyFlag = 2;
}
else
{
if(ucKeyFlag)
{
if(ucKeyFlag == 2)
{
if(ucKeyTemp == ENTER) ucKeyCode = ENTER;
}
ucKeyFlag = 0;
}
else
{
if(ucKeyTemp == 0x70) ucKeyCode = N0;
else if(ucKeyTemp == 0x69) ucKeyCode = N1;
else if(ucKeyTemp == 0x72) ucKeyCode = N2;
else if(ucKeyTemp == 0x7A) ucKeyCode = N3;
else if(ucKeyTemp == 0x6B) ucKeyCode = N4;
else if(ucKeyTemp == 0x73) ucKeyCode = N5;
else if(ucKeyTemp == 0x74) ucKeyCode = N6;
else if(ucKeyTemp == 0x6C) ucKeyCode = N7;
else if(ucKeyTemp == 0x75) ucKeyCode = N8;
else if(ucKeyTemp == 0x7D) ucKeyCode = N9;
else if(ucKeyTemp == 0x05) ucKeyCode = F1;
else ucKeyCode = ucKeyTemp;
}
}
}
++ucKeyPulse;
}
else
if( ucKeyPulse == 10) // Startbit
{
if( bit_is_clear(PIND, KB_DAT) ) ucKeyPulse = 0;
}
}
#endif
// 1602LCD_H
#ifndef _1602LCD_H
#define _1602LCD_H
// PORTD
// 1: E read(1->0); write(1);
//
// 0: RS 0: read( p->address counter )
// write( p->command register)
// 1: p->data register
// 4-7: DATA
#define LCD_PORT PORTD
#define LCD_DDR DDRD
#define LCD_E 1
#define LCD_RS 0
#define NumL 4
#define LCD_DELAY_TIME 2
extern void LCD_Init();
extern void LCD_DisplayString(unsigned char X, unsigned char Y, unsigned char* uString);
extern void LCD_DisplayNum(unsigned char X, unsigned char Y, unsigned int ulNum);
void LCD_Clean_Screen();
void LCD_Set_XY(unsigned char X, unsigned char Y);
void LCD_Send_Command(unsigned char udata, unsigned char uCode);
#define sbi(Port, Bit) (Port |= (1<<Bit))
#define cbi(Port, Bit) (Port &= ~(1<<Bit))
#define eoi(Port, Bit) (Port ^= (1<<Bit))
#define IsTrue(Pin, Bit) (Pin & (1<<Bit) )
#endif
// 1602LCD.c
// ************************************ 1602 LCD Drive ************************************
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "1602LCD.h"
#include "RTOS_T0.h"
// uCode = 0: Send Command
// uCode = 1: Send Data
void LCD_Send_Command(unsigned char udata, unsigned char uCode)
{
if(uCode) sbi(LCD_PORT, LCD_RS);
else cbi(LCD_PORT, LCD_RS);
LCD_PORT &= 0x0F; // HIGH
LCD_PORT |= (udata & 0xF0);
sbi(LCD_PORT, LCD_E);
cbi(LCD_PORT, LCD_E);
LCD_PORT &= 0x0F; // LOW
LCD_PORT |= (udata << 4);
sbi(LCD_PORT, LCD_E);
cbi(LCD_PORT, LCD_E);
cbi(LCD_PORT, LCD_RS);
delay_ms(LCD_DELAY_TIME);
}
void LCD_Clean_Screen()
{
LCD_Send_Command(0x01, 0);
}
void LCD_Set_XY(unsigned char X, unsigned char Y)
{
if( Y ) X = 0xc0 + X;
else X = 0x80 + X;
LCD_Send_Command(X, 0);
}
void LCD_DisplayString(unsigned char X, unsigned char Y, unsigned char* uString)
{
LCD_Set_XY(X, Y);
while( *uString )
{
LCD_Send_Command(*uString, 1);
*uString++;
}
}
void LCD_DisplayNum(unsigned char X, unsigned char Y, unsigned int ulNum)
{
LCD_Set_XY(X, Y);
unsigned char i;
unsigned char charNum[NumL];
for(i=0; i < NumL; i++)
{
charNum[NumL-1-i] = 0x30 + ulNum%10;
ulNum = ulNum/10;
}
for(i = 0; i < NumL-1; i++)
{
if(charNum != 0x30) break;
}
for(; i < NumL; i++)
{
LCD_Send_Command(charNum, 1);
}
}
void LCD_Init()
{
LCD_DDR = 0b11110011;
delay_ms(500);
LCD_Send_Command(0x33, 0);
LCD_Send_Command(0x32, 0);
LCD_Send_Command(0x28, 0);
LCD_Send_Command(0x0C, 0);
LCD_Clean_Screen();
}
// ************************************ LCD Drive End ************************************
RTOS部分就是前面提到的那些了,仅参考 |
|