//*********************************************
#include <mega128.h>
#include <Stdio.h> //头文件
#include <Stdlib.h>
#include <string.h>
//****************************************
//*****************************************
#define Zone_Number 15
#define nop #asm("nop");
#define clrwdt #asm("wdr");
#define TOIE1 2 // bit position Time1 overflow Interrupt
#define TOIE0 0 // bit position Time0 overflow Interrupt
#define TOIE2 6 // bit position Time0 overflow Interrupt
#define RXEN 4 //接收数据允许
#define TXEN 3 //发送数据允许
#define PC0 0 //定义各个I/O口
#define PC1 1
#define PC2 2
#define PC3 3
#define PC4 4
#define PC5 5
#define PC6 6
#define PC7 7
#define PB0 0
#define PB1 1
#define PB2 2
#define PB3 3
#define PB4 4
#define PB5 5
#define PB6 6
#define PB7 7
#define PD0 0
#define PD1 1
#define PD2 2
#define PD3 3
#define PD4 4
#define PD5 5
#define PD6 6
#define PD7 7
#define PE0 0
#define PE1 1
#define PE2 2
#define PE3 3
#define PE4 4
#define PE5 5
#define PE6 6
#define PE7 7
#define PF0 0
#define PF1 1
#define PF2 2
#define PF3 3
#define PF4 4
#define PF5 5
#define PF6 6
#define PF7 7
//******************************************************
/*
//各个I/O口使用功能说明。
PE6: PowerFault
PE7: SystemFault
PB0: CH16
PB1: CH15
PB2: CH14
PB3: CH13
PB4: CH12
PB5: CH11
PF1: CH10
PF0: CH09
PC4: CH1
PC3: CH2
PC2: CH3
PC1: CH4
PC0: CH5
PD5: CH6
PD6: CH7
PD7: CH8
PD4: Control for 2#RS485
USART2: RXD1 , PD2
USART2: TXD1 , PD3
PB6: Control for 1#RS485
USART1: RXD0 , PE0
USART1: TXD0 , PE1
if ( !PINC.PC4 ) iword |= 0b01 ; // 这句话的定义没看懂、、、、、、、、、、、、、、、、、、、、、、、、、、、
if ( !PINC.PC3 ) iword |= (0b01<<1 ) ;
if ( !PINC.PC2 ) iword |= (0b01<<2 ) ;
if ( !PINC.PC1 ) iword |= (0b01<<3 ) ;
if ( !PINC.PC0 ) iword |= (0b01<<4 ) ;
if ( !PIND.PD5 ) iword |= (0b01<<5 ) ;
if ( !PIND.PD6 ) iword |= (0b01<<6 ) ;
if ( !PIND.PD7 ) iword |= (0b01<<7 ) ;
if ( !PINF.PF0 ) iword |= (0b01<<8 ) ;
if ( !PINF.PF1 ) iword |= (0b01<<9 ) ;
if ( !PINB.PB5 ) iword |= (0b01<<10 ) ;
if ( !PINB.PB4 ) iword |= (0b01<<11 ) ;
if ( !PINB.PB3 ) iword |= (0b01<<12 ) ;
if ( !PINB.PB2 ) iword |= (0b01<<13 ) ;
if ( !PINB.PB1 ) iword |= (0b01<<14 ) ;
if ( !PINB.PB0 ) iword |= (0b01<<15 ) ;
*/
//************************
//////////////////////////////
//***
//*** //各个函数声明
//****
void clik_to_send(void) ;
void UART_Initial(void) ;
void UART_sent(void) ;
void delay_1s(void) ;
void send_no_alarm(void) ;
void at_once_alarm(void) ;
void prepare_to_send1(void) ;
//***************************
void read_kbd(void) ;
unsigned int KBD_Any , iword ;
unsigned char DIGIN_array[16] ;
unsigned int alarm_start , alarmed , alarm_end ;
//***************************
void initial_MCU(void) //初始化MCU
{
//***
WDTCR = 0b00001110; //Set Watch Dog Interval 64ms
//TCCR0 = 0b00000100;
//Set TCCR0 as 100(256 divide CK=4M ->0.25 us * 256 = 64 us
//192 = $C0 ,62"(256-192)" * 64 us = 3.968 ms, Timer and Count
//100 = 256 -156 , 156*64us = 9.98ms
//80 * (256 -61) = 80 * 195 = 19650
//19650 * 64 us = 99840
//*************initial TCCR1, 16bit time count
TCCR0 = 0b00000110; //外部T0脚 下降沿驱动
/* Prescale
000 : , 001
010 : 8 ,
011 : 64 ,
100 : 256 , 101 : 1024
110 , 111 : External
10 ms = 156 * 0.25 * 256 (us)
100: 256 Prescal
*/
//*************
DDRC = 0b00000000 ; //设置C口为输入方向
PORTC = 0b00000011; //设置C口最低2位为高电平
DDRD = 0b00001000 ; //设置D口
PORTD = 0b11110111 ;
DDRB = 0b01000000 ; //设置B口
PORTB = 0b10111111 ;
DDRE = 0b00000010 ; //设置E口
PORTE = 0b111111101 ;
DDRF =0b00000000 ; //设置F口
PORTF =0b00000011 ;
//UART_Initial() ;
nop ;
}
//****//****//****//****//****//****//****//**** //设置延时
void delay_1ms(void)
{
unsigned int i ;
for ( i= 0 ;i<1000;i++)
{clrwdt;}
}
void us_delay(void )
{
#asm("nop");
#asm("nop");
#asm("nop");
#asm("nop");
}
void delay_100us(void)
{
unsigned char i ;
for (i = 0 ; i < 100 ; i++ )
{;}
}
// *********************
#define Preset_ratio 100 // 256 - 100 = 156 => 156 * 256 * 0.25 us =10ms
unsigned char second = 0 , min = 0 , hour=0 , sec_clik , second5= 0 ,second10_clik;
unsigned int ms100_count = 0 , ms_count = 0 ;
unsigned char ms10_count = 0 ;
unsigned char sec_i_current ;
void LED_comm_process(void) ;
void time_count(void)
{
unsigned int i;
i = TCNT0 ; //TC0计数值寄存器
while(TCNT0)
{
clrwdt; //什么意思?。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
}
TCNT0 = (Preset_ratio ) ; // modify 6 //加括号干嘛
i = TCNT0 ;
clrwdt;
sec_clik = 0 ;
second10_clik = 0 ;
ms100_count ++ ;
{
if ( ms100_count > 99 ) //什么意思
{
sec_clik = 1 ;
ms100_count = 0 ;
second ++ ;
if ( min > 59 ) { min =0 ; }
if ( hour > 59 ) { hour = 0 ; }
if ( second > 59 )
{
second =0 ;
min ++ ;
if ( min > 59 )
{
min = 0 ;
hour++ ;
if ( hour >59 )
{
hour = 0 ;
}
}
}
}
}
ms10_count ++ ;
if ( ms10_count > 9 )
{
ms10_count = 0 ;
}
if ( sec_clik )
{
second5 ++ ;
if ( second5 == 10 )
{
second10_clik = 1 ;
second5 = 0 ;
}
}
//**************
read_kbd() ; //读输入状态
//**************
//**************
}
void delay_1s(void)
{
sec_clik = 0 ;
ms100_count = 0 ;
while(!sec_clik)
{
time_count() ;
}
}
void delay_200ms(void)
{
sec_clik = 0 ;
ms100_count = 0 ;
while(ms100_count<20)
{
time_count() ;
}
}
void delay_100ms(void)
{
sec_clik = 0 ;
ms100_count = 0 ;
while(ms100_count<10)
{
time_count() ;
}
}
void delay_50ms(void)
{
sec_clik = 0 ;
ms100_count = 0 ;
while(ms100_count<5)
{
time_count() ;
}
}
void main(void)
{
//************************
initial_MCU() ; //初始化MCU
while(1)
{
//***
//**
time_count() ;
if ( (alarm_start) | (alarm_end ) )
{
at_once_alarm() ;
}
if (second10_clik)
{
clik_to_send() ;
}
//**
}
#asm("jmp 0x00") ; // restart
}
//**********************************
unsigned char fram_state , fram_buf[16] ;
#define mark_no_received fram_state = 0 ;
#define mark_received_completion fram_state = 100 ;
#define URSEL 6
sfrb UBRR =9;
sfrb UCR =0xa;
sfrb UDR = 0x0c ;
#define RXEN 0
#define TXEN 1
#define RXCIE0 7
#define enable_rxci UCSR0B |= 1<< RXCIE0 ; // 允许接收数据中断
#define disable_rxci UCSR0B = ((UCSR0B) & (0x7f)) ;// 禁止接收数据中断
//#define set_485_transmit PORTB.6 = 1 ; disable_rxci ;//set RS485 as transmit
//#define set_485_recieve PORTB.6 = 0 ; enable_rxci ;//set RS485 as receive
#define set_485_transmit PORTD.4 = 1 ; disable_rxci ;//set RS485 as transmit
#define set_485_recieve PORTD.4 = 0 ; enable_rxci ;//set RS485 as receive
// PD7 is used to control RS485 , RE or TE ; PD7 = 0 对应 发送,PD7 = 1 对应接受
/*
UCSRA : P151
Bit7: USART receive Complete
Bit6: USART Transmit Complete
Bit5: USART Data Register Empty
Bit3: Frame Error
Bit2: Parity Error
Bit1: