📄 wdg1.c
字号:
//Target : atmega16
//crystal:4MHZ
//time dog
#define F_CPU 4000000UL
#include <inttypes.h>
#include <string.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
//#include <util/delay.h>
#include <avr/wdt.h>
#include <avr/io.h>
#include <avr/sfr_defs.h>
//#include <avr/eeprom.h>
#include <avr/pgmspace.h>
#define TRUE 1
#define FALSE 0
//#define AD_MID 0x0262 //1.525V
//#define AD_MID 0x288 //1.62V
//#define AD_MID 0x284 //1.61V
//#define AD_MID 0x280 //1.60V
#define AD_MID 0x272 //1.58V
//#define AD_MID 0x260 //1.54V
//#define AD_MID 0x252 //1.52V
/******************timerone***********************/
#define EN_TIMERONE_INT (TIMSK = _BV(TOIE1))
#define DIS_TIMERONE (TCCR1B = (TCCR1B & 0xf8))
#define EN_TIMERONE (TCCR1B = (TCCR1B | 0x03))
#define DELAY100H 0xF3 //50ms
#define DELAY100L 0xCA
/******************count clock*******************/
//#define ONE_MINUTES 60
#define SECONDS 20
//#define ONE_HOURS 24
/******************Usart init*******************/
#define BAUDRATEH 0X00 //9600
#define BAUDRATEL 0X19
/******************port init*******************/
#define DIS_Relay1 PORTA |= (1<<PA3)
#define DIS_Relay2 PORTA |= (1<<PA4)
#define EN_Relay1 PORTA &= ~(1<<PA3)
#define EN_Relay2 PORTA &= ~(1<<PA4)
#define SB_Working PORTB |= (1<<PB1)
#define CB_Working PORTB &= ~(1<<PB1)
#define SB_WDI PORTB |= (1<<PB3)
#define CB_WDI PORTB &= ~(1<<PB3)
#define SB_RW PORTD |= (1<<PD3)
#define CB_RW PORTD &= ~(1<<PD3)
#define SB_RS PORTD |= (1<<PD5)
#define CB_RS PORTD &= ~(1<<PD5)
#define SB_TXRXLED PORTD |= (1<<PD6)
#define CB_TXRXLED PORTD &= ~(1<<PD6)
#define SB_BUZZER PORTD |= (1<<PD7)
#define CB_BUZZER PORTD &= ~(1<<PD7)
#define SB_E PORTA |= (1<<PA5)
#define CB_E PORTA &= ~(1<<PA5)
uint8_t Second_Count;
uint8_t Second_Timer;
uint8_t Minute_Timer;
uint8_t Hour_Timer;
uint8_t Second_Over;
uint8_t Minute_Over;
uint8_t Hour_Over;
uint8_t Work_Led_Flag;
uint8_t Task_No;
uint8_t Task_Lock;
uint8_t Task_Select;
uint8_t Task_Begin;
uint8_t Buzzer_Flag;
uint8_t Buzzer_Timer;
uint8_t PORT_Temp;
uint8_t Working_Timer;
uint8_t Reset_Flag;
uint8_t Reset_Timer;
uint8_t Time_Reset_Begin;
uint8_t image1;
uint8_t image2;
uint8_t image_timer;
uint8_t Net_Error_Count;
uint8_t Net_Error;
uint8_t Net_Check_Timer;
uint8_t Net_Check_Timer;
uint8_t Net_Check_Begin;
uint8_t Display_Check;
uint8_t LCD_State;
uint8_t Test_Status_Flag;
uint8_t Set_Status_Flag;
uint8_t Set_Count;
uint8_t Test_Count;
uint8_t Alarm_Flag;
uint8_t Spark_Count;
uint8_t Spark_Flag;
uint8_t Exit_Interface_Flag;
uint8_t Net_Alarm_Flag;
uint8_t Check_Begin;
uint8_t ADC_resalt;
uint8_t Plus_Count;
uint16_t LCD_Start_Flag;
uint8_t LCD_Shift_Flag;
uint8_t LCD_Shift_Timer;
uint16_t AD_Result;
uint8_t AD_ResultH,AD_ResultL;
uint8_t Task_Buff[20];
uint8_t Task_Lock;
uint8_t Task_Front,Task_Rear;
uint16_t Check_Timer;
void DelayMs(unsigned int t)
{
unsigned int i;
for(i=0;i<t;i++)
{
_delay_loop_2(4*250);
}
}
static void Timerone_Init(void)
{
TCCR1A = 0;
TCCR1B = _BV(CS11)|_BV(CS10);
SFIOR = _BV(PSR10);
TIFR = _BV(TOV1);
TCNT1H = DELAY100H;
TCNT1L = DELAY100L; //50ms
TIMSK = _BV(TOIE1);
}
void Port_Init(void)
{
DDRA = (1<<DDA5)|(1<<DDA4)|(1<<DDA3)|(1<<DDA2)|(1<<DDA1)|(1<<DDA0);
PORTA = 0x00;
PORTA = (1<<PA5);
DDRB = (1<<DDB3)|(1<<DDB1);
PORTB = 0x00;
DDRC = 0xFF;
PORTC = 0x00;
DDRD = (1<<DDD7)|(1<<DDD6)|(1<<DDD5)|(1<<DDD4)|(1<<DDD3);
PORTD = (1<<PD5)|(1<<PD4)|(1<<PD3)|(1<<PD2);;
}
void USART_Init( uint8_t BAUDH,uint8_t BAUDL )
{
UBRRL = BAUDL;
UBRRH = BAUDH;
UCSRB = (1<<TXEN);
// UCSRC = (1<<URSEL)|(1<<USBS)|(1<<UCSZ1)|(1<<UCSZ0);
UCSRC = 0x86;
}
void LCD_Com(uint8_t Command)
{
CB_RS;
CB_RW;
SB_E;
PORTC = Command;
// asm("nop");
// asm("nop");
CB_E;
}
void LCD_Init(void)
{
LCD_Com(0x30); //set 8-bit parall.
DelayMs(1);
LCD_Com(0x30); //set 8-bit parall.
DelayMs(1);
LCD_Com(0x0C); //set display on.
DelayMs(1);
LCD_Com(0x01); //clear the sreen.
DelayMs(10); //set the dot.
LCD_Com(0x06);
}
void LCD_Write(unsigned char Data,uint8_t Command_Data) //row is the 80h-87h,90h-97h
{
DDRC = 0xFF;
if(Command_Data == 0) // write the Command and add.
{
CB_RS;
}
if(Command_Data == 1) // write the data.
{
DelayMs(1);
SB_RS;
}
CB_RW;
SB_E;
PORTC = Data;
CB_E;
SB_RW;
}
void LCD_Display(unsigned char Data[16],uint8_t Row)
{
uint8_t i;
LCD_Write(0x0c,0);
DelayMs(1);
if(Row == 1)
{
LCD_Write((0x80),0);
for(i = 0;i <= 15;i++)
// for(i=0;Data[i]!='\0';i++)
{
LCD_Write(Data[i],1);
}
}
if(Row == 2)
{
LCD_Write((0x90),0);
// for(i=0;i<=15;i++)
for(i = 0;Data[i] != '\0';i++)
{
LCD_Write(Data[i],1);
}
}
}
void USART_Transmit(uint8_t data)
{
while ( !( UCSRA & (1<<UDRE)) );
UDR = data;
}
void Reset(void)
{
Reset_Timer = 0;
DIS_Relay2;
}
/*uint8_t Image_Check(void)
{
uint8_t Image_Temp;
if(Check_Timer >= 100) //if the time > 5s
{
Check_Timer = 0; //timer count reset.
if(Plus_Count < 2)
{
Plus_Count = 0; //Plus_Count reset
image1 = 0;
image2 = 0;
return 1;
}
Plus_Count = 0; //Plus_Count reset
image1 = 0;
image2 = 0;
return 0;
}
else
{
image1 = PINB;
image1 &= 0x10;
if(image1 ^ image2)
{
DelayMs(1);
Image_Temp = PINB;
Image_Temp &= 0x10;
if(image1 & Image_Temp)
{
Plus_Count++;
}
image2 = image1;
}
else
{
image2 = image1;
}
return 0;
}
}
*/
void ADC_Init(void)
{
ACSR = (1<<ACD);
ADMUX = (1<<REFS1)|(1<<REFS1)|(1<<MUX2)|(1<<MUX1)|(1<<MUX0);
ADCSRA = (1<<ADEN)|(1<<ADSC)|(1<<ADPS1)|(1<<ADPS2);
}
uint8_t Net_Check(void)
{
AD_ResultL = ADCL;
AD_ResultH = ADCH;
AD_Result = AD_ResultH;
AD_Result = AD_Result << 8;
AD_Result = AD_Result + AD_ResultL;
if(Net_Check_Timer >= 70) //if the time > 3s
{
Net_Check_Timer = 0;
if(Net_Error_Count >= 7)
{
Net_Error_Count = 0;
Net_Error = 1;
return 1;
}
Net_Error = 0;
Net_Error_Count = 0;
return 0;
}
else
{
if(AD_Result > AD_MID)
{
Net_Error_Count++;
}
return 0;
}
}
void Task_In(void)
{
if(Task_Rear < 20)
{
Task_Lock = 0;
Task_Buff[Task_Rear] = Task_No;
Task_Lock = 1;
Task_Rear++;
}
if(Task_Rear >= 20)
{
Task_Rear = 0;
}
// USART_Transmit(Task_Rear);
// USART_Transmit(Task_Buff[Task_Rear-1]);
}
uint8_t Task_Out(void)
{
uint8_t Task_Select=0;
if(Task_Lock == 1) //enable to read the task number.
{
if(Task_Buff[Task_Front] != 0)
{
Task_Select = Task_Buff[Task_Front];
Task_Buff[Task_Front] = 0;
Task_Front++;
}
// USART_Transmit(Task_Front);
// USART_Transmit(Task_Buff[Task_Front-1]);
if((Task_Front > Task_Rear)||(Task_Front >= 20))
{
Task_Front = 0;
Task_Rear = 0;
}
return (Task_Select);
}
return 0;
}
SIGNAL(SIG_OVERFLOW1)
{
TCNT1H = DELAY100H;
TCNT1L = DELAY100L; //50ms
Task_No = 0;
Second_Count++;
if(Second_Count >= SECONDS) //the timer to count 24 hour
{
Second_Timer++;
if(Second_Timer == 60 )
{
Minute_Timer++;
Second_Over = 1;
if( Minute_Timer == 60 )
{
Hour_Timer++;
Minute_Over = 1;
if( Hour_Timer == 24 )
{
Hour_Over = 1;
}
}
}
}
//LCD task timer
if(LCD_Start_Flag != 0xffff)
{
LCD_Start_Flag++;
}
else
{
LCD_Start_Flag = 0xffff;
}
//handing the some flags
//working led
Working_Timer++;
if(Working_Timer < 20)
{
Work_Led_Flag = 0;
}
if((Working_Timer >= 20) &&(Working_Timer < 40))
{
Work_Led_Flag = 1;
}
if(Working_Timer == 40)
{
Working_Timer = 0;
}
//however the time, the image Check_Timer ++, and if >100 equle=0
if(Task_Begin == 1)
{
Check_Timer++;
}
if(Net_Check_Begin == 1)
{
Net_Check_Timer++;
}
//the relay open time
if(Reset_Timer < 40) //the Reset_Timer < 0xff when the relay colse.
{
Reset_Timer++;
}
if(Reset_Timer >= 40) //if the relay2 equl 40 time > 1s.
{
EN_Relay2;
Reset_Timer = 0xff;
}
if((Second_Timer == 60)&&(Check_Begin < 30))
{
Check_Begin++;
}
//select the Task number
if(Task_Rear <= 20 )
{
/* if((Second_Count % 3 == 0)&&(Check_Begin >= 30))
{
Task_No = 1;
Task_Begin = 1; //image check
Task_In();
Check_Begin = 30;
}*/
if((Second_Count % 5 == 0)&&(LCD_Start_Flag > 82))
{
Task_No = 2; //net check Task
Net_Check_Begin = 1;
Task_In();
}
if((Hour_Timer == 6)&&(Minute_Timer == 1)&&(Second_Timer == 1)&&(Second_Count >= (SECONDS-1)))
{
Task_No = 3; //reset all Task
Task_In();
}
//select the LCD task number;
if(LCD_Start_Flag == 1) //Init the Lcd
{
Task_No = 4;
Task_In();
}
if(LCD_Start_Flag == 2) //display lcd screen 1
{
Task_No = 5;
Task_In();
}
if(LCD_Start_Flag == 62) //display lcd screen 2
{
Task_No = 6;
Task_In();
}
if(LCD_Start_Flag == 122) //display lcd screen 3
{
Task_No = 7;
Task_In();
}
if(LCD_Start_Flag == 182) //display lcd screen 4
{
Task_No = 8;
Task_In();
LCD_Start_Flag = 0xffff;
LCD_Shift_Flag = 1;
}
if((Net_Error)&&(Display_Check)) //after display the check deives ,display the Net Error.
{
Task_No = 12;
Task_In();
}
//shift display the srceen "working..."
if(Net_Error != 1)
{
if((LCD_Shift_Flag == 1)&&(Second_Count >= SECONDS))
{
LCD_Shift_Timer++;
LCD_Shift_Timer %= 4;
}
if((LCD_Shift_Flag == 1)&&(LCD_Shift_Timer == 0))
{
Task_No = 8;
Task_In();
}
if((LCD_Shift_Flag == 1)&&(LCD_Shift_Timer == 1))
{
Task_No = 9;
Task_In();
}
if((LCD_Shift_Flag == 1)&&(LCD_Shift_Timer == 2))
{
Task_No = 10;
Task_In();
}
if((LCD_Shift_Flag == 1)&&(LCD_Shift_Timer == 3))
{
Task_No = 11;
Task_In();
}
}
}
//Task_Lock = 1;
//Task lock flag locked after select the Task number to ensure the Task execute.
//clean the time over flags and the timer
if(Second_Count == 20)
{
Second_Count = 0;
}
if(Second_Over==1)
{
Second_Over= 0;
Second_Timer=0;
}
if(Minute_Over==1)
{
Minute_Over= 0;
Minute_Timer=0;
}
if(Hour_Over==1)
{
Hour_Over= 0;
Hour_Timer=0;
}
}
int main(void)
{
uint8_t temp;
Second_Count =0;
Second_Timer =0;
Minute_Timer =0;
Hour_Timer = 0;
Second_Over = 0;
Minute_Over = 0;
Hour_Over = 0;
Task_Front = 0;
Task_Rear = 0;
Test_Status_Flag = 0;
Set_Status_Flag = 0;
temp = 0;
Task_No = 0;
Reset_Timer = 0xff;
Task_Begin = 0;
Check_Timer = 0;
Plus_Count = 0;
image1 = 0;
image2 = 0;
Time_Reset_Begin = 0;
Net_Check_Begin = 0;
Net_Check_Timer = 0;
Net_Error_Count = 0;
Working_Timer = 0;
Work_Led_Flag = 0;
Check_Begin = 0;
LCD_Shift_Timer = 0;
LCD_State = 1;
LCD_Start_Flag = 0;
Net_Error = 0;
Display_Check = 0;
Task_Lock = 0; //0----enable write, 1----enable;
cli();
Port_Init();
ADC_Init();
CB_WDI;
SB_WDI;
CB_WDI;
EN_Relay2;
// DIS_Relay2;
USART_Init(BAUDRATEH,BAUDRATEL);
Timerone_Init();
wdt_enable(WDTO_2S);
wdt_reset();
sei();
while(1)
{
wdt_reset(); //reset the watchdog
CB_WDI;
SB_WDI; //reset the exp_watchdog
CB_WDI;
// USART_Transmit(Reset_Timer);
if(Work_Led_Flag == 0)
{
CB_Working;
}
if(Work_Led_Flag == 1)
{
SB_Working;
}
Task_Select = Task_Out();
switch(Task_Select)
{
/* case 1: //Image_Check
{
if(Image_Check())
{
Reset();
}
break;
}*/
case 2:
{
Net_Check(); //net check
if(Net_Error)
{
SB_TXRXLED;
}
else
{
CB_TXRXLED;
}
ADC_Init();
break;
}
case 3: //Reset
{
Reset();
break;
}
case 4:
{
LCD_Init();
break;
}
case 5:
{
LCD_Display("工业图像服务器 ",1);
LCD_Display(" 启动中...... ",2);
break;
}
case 6:
{
LCD_Display("程序加载中......",2);
break;
}
case 7:
{
LCD_Display("正在自测请稍候!",2);
Display_Check = 1;
break;
}
case 8:
{
LCD_Display("工业图像服务器 ",1);
LCD_Display(" 正常运行 ",2);
break;
}
case 9:
{
LCD_Display("工业图像服务器 ",1);
LCD_Display(" 正常运行. ",2);
break;
}
case 10:
{
LCD_Display("工业图像服务器 ",1);
LCD_Display(" 正常运行.. ",2);
break;
}
case 11:
{
LCD_Display("工业图像服务器 ",1);
LCD_Display(" 正常运行... ",2);
break;
}
case 12:
{
LCD_Display("网络接口连接错误",1);
LCD_Display("请检查网络插座! ",2);
break;
}
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -