/*
************************************************************
*project : DMX light
*author : PJY
*data : 2017-2-9
*last time :
*MCU : STM8S103F3
*package : SSOP20
*********************option******************************
********************版本更改记录*************************
//#define edition 00 //版本
//1>取消改自动收�
// 每次发送10个频道
//2>
//---------------------备注----------------------------//
*********************************************************
*/
//#pragma rambank0 //变量在区块0
//#pragma norambank
//***********************编译选项************************//
#include "main.h"
SystemFlags Flags;
g_count count;
g_count0 count0; //**数组太大了
suchar suchar_key;
//suchar suchar_key_delay;
suchar suchar_mode_t;
static unsigned char test;
static unsigned char delay_s;
static unsigned char TX_overtime;//发送超时
static unsigned char rest_up;//重启标志
const ulong MCU_serial_number[1]={0x00000000};//存MCU序列号,用作遥控地址码。
//=====================================================
//-------------------上电初始化------------------------//
void HWInit(void)
{
//TX_overtime=1;
_wdtc=0xa8;//看门狗除能
//--------------------
test=0;
delay_s=0;
//==============================================
//----------I/O口上电复位---------------------//
//==============================================
//--------------通用寄存器清零bank0,bank1,bank2,0x60-0x7FH
for(_mp0=0x40;_mp0<0xdf;_mp0++)
{
_iar0=0x00 ;
}
_iar0=0x00 ;
//--------------休眠模式0
_smod = 0x01 ;//8mHZ
_smod1 = 0x00 ;//
//--------------LVD关
// _lvdc = 0x00 ;
//--------------AD关
_pasr=0x00;
_pbsr=0x00;
//--------------I/O口开机状态设定
_pac=0xff ;
_papu=0xff ;//给上拉电阻b=11111111
_pa=0x00 ;
_pawu=0xff ;//开通PA0,1,2,5唤醒
_pbc=0x00 ;
_pbpu=0x00 ;//给下拉电阻b=000
_pb=0x00 ;
//_pbwu=0x04 ;//开通PA0,1,2,5唤醒
_pcc=0x00 ;
_pcpu=0x00 ;//给下拉电阻b=000
_pc=0x00 ;
//--------------时基
//_tbc=0xcf ; //开 周期是2^15/2mHZ=8.192us
_tbc=0xc3 ; //开 周期是c0-2^8/2mHZ=128us,c3-2^8/2mHZ=1.24Ms
//--------------set TM0
_ptm0c0=0x20 ; //DISABLE CTMCTM CLOCK SOURCE FSYS/16=125kHZ ,关闭定时器
_ptm0c1=0xc1 ; //TIME MODE, CCRA COMPARE CLEAR THE COUNTER. 匹配A,定时、计数模式
_ptm0al=0x00 ; //62.5us interrupt / one time
_ptm0ah=0x00 ; //
_intc0=0x10 ; //DISABLE INTERRUPT
_pt0on=0 ; //START CTM 0
_mf0e=1 ; //ENABLE CTM INTERRUPT
_ptma0e=1 ; //ENABLE CTM CCRA COMPARE INTERRUPT
_enadc=0 ;
_tb0e=1 ;
_emi=1 ; //ENABLE EMI
//------------------------------------------
io_out_CE;
#if model_tx
io_CE_0 ; //
#else
io_CE_1 ; //
#endif
io_CSN_0;
//io_CSN_1; //
io_MOSI_0;
io_SCK_0;
io_input_IRQ; //中断输入
io_led_r_off; //
//io_led_r_on; //
io_papu4_stop_1; //按键上拉使能
io_papu5_open_1; //
io_papu6_close_1; //
io_pcpu0_miso;
io_input_miso;
//-----------------------------------------------------------
io_input_stop;
io_input_open;
io_input_close;
//-------------
suchar_key=0;
//suchar_key_delay=0;
}
//=====================================================
#pragma vector ISR_tmr0@0x08
void ISR_tmr0(void)
{
Flags.t_1ms=1;
if(TX_overtime>1)TX_overtime--;
//timer();
}
//---------------write EEPROM------------------------//
void write_eeprom(unsigned int address,unsigned char Data)
{
//FLASH_Unlock(FLASH_MEMTYPE_DATA);
//FLASH_ProgramByte(address, Data);
//FLASH_Lock(FLASH_MEMTYPE_DATA);
_eea=address&0x00ff;//低8位地址
_eed=Data; //写数据
_eec=0X08; //写使能
_bp=0x01; //区块1,解锁
_eec=_eec|0x4; //写操作
wring:
if(!(_eec&0x04)) goto wring;
_eec=0;
_bp=0x0; //区块0,
}
//=====================================================
//---------------------read EEPROM---------------------
u8 read_eeprom(unsigned int address)
{
unsigned char r_byte ;
//FLASH_Unlock(FLASH_MEMTYPE_DATA);
//r_byte = FLASH_ReadByte(address);
//return(r_byte);
_eea=address&0x00ff;//低8位地址
_eec=0X02; //读使能
_bp=0x01; //区块1,解锁
_eec=_eec|0x1; //读操作
rding:
if(!(_eec&0x01)) goto rding;
_eec=0;
_bp=0x0; //区块0,
r_byte=_eed; //数据
return(r_byte);
}
//=============================================
//===============时钟处理======================
void timer(void) //100us
{
/*
static u8 count_t0=0 ; //计时累加器
static u8 count_t1=0 ; //计时累加器
static u8 count_s=0 ; //秒计数器
static u8 count_m=0 ; //分计数器
static u8 count_h=0 ; //
static u8 count_200ms=0; //
static u8 count_2s=0; //
static u8 count_10ms=0;
static u8 count_50ms=0;
static u8 count_100ms=0;
static u8 count_500ms=0 ;//
static u8 count_beep=0;
*/
static u8 count_t0; //计时累加器
static u8 count_t1; //计时累加器
static u8 count_s; //秒计数器
//static u8 count_m; //分计数器
//static u8 count_h; //
static u8 count_200ms; //
static u8 count_2s; //
static u8 count_10ms;
static u8 count_50ms;
static u8 count_100ms;
static u8 count_500ms;//
static u8 count_beep;
//-------------------------------
if(Flags.t_1ms==1)
{
Flags.t_1ms=0;
//------------------------变频定时
if(++count.change_CH > data_change_CH)
{
count.change_CH=0;
Flags.change_CH=1;
}
//------------------------
if(++count_10ms>9)
{ count_10ms=0;
Flags.t_10ms=1;
if(suchar_mode_t>1)suchar_mode_t--;
}
//------------------------
if(++count_t0>=100) //100MS
{ count_t0=0 ;
Flags.t_100ms=1;
count_t1++;
//------------------------
if(++count_500ms>4)//500ms
{ count_500ms=0;
Flags.t_500ms=1;
}
}
//------------------------count_t1=10=1秒
if(count_t1>=10)
{ count_t1=0 ;
count_s++ ;
//----------------------------------
Flags.t_1s=1 ;
}
//------------------------
/*
if(count_s>=60) //分钟
{
count_s=0 ;
count_m++ ;
}
//------------------------
if(count_m>=60) //小时
{
count_m=0 ;
count_h++ ;
//----------------------
}
*/
}
}
//==============================================
//------------------CRC-------------------------
//==============================================
u16 crc_chk(u8 *data, u8 length)
{
u8 i;
u8 j;
u8 sdata;
u8 tmp;
u16 iResulrCrc = 0xffff;
if (length != 0)
{
for (i = 0; i < length; i++)
{
sdata = *(data + i);
for (j = 0; j < 8; j++)
{
tmp = iResulrCrc % 2 + sdata % 2;
iResulrCrc = iResulrCrc / 2;
sdata = sdata / 2;
if(tmp == 1)
iResulrCrc = iResulrCrc^0xa001;
}
}
}
return iResulrCrc;
}
//=====================================================
//-----------------------------------------------------
u8 crc_8(u8 *data,u8 len)
{
u8 i=0;
u8 chksum=0;
for (i=0;i<len-1;i++)
{
chksum += data[i];
}
//chksum= 0x100 - chksum;
return chksum;
}
//====================================================
u16 delay(u16 n)
{
u16 i;
for(i=0;i<n;)
{
i++;
}
return 0;
}
//====================================================
unsigned char SPI_SendByte(unsigned char byte)//SPI收发
{
unsigned char i;
for(i = 0; i < 8; i++)
{
if (byte & 0x80)
io_MOSI_1;
else
io_MOSI_0;
byte <<= 1;
io_SCK_1;
if (io_MISO_rd)
byte++; //接收数据
io_SCK_0;
}
return byte;
}
//====================================================
//寄存器访问函数:用来设置24L01?的寄存器的值。基本思路就是通过WRITE_REG?命令(也?
//就是0x20+寄存器地址)把要设定的值写到相应的寄存器地址里面去,并读
评论0