📄 main.c.bak
字号:
//c8051 datasheet推荐的系统时钟选择流程
//Step 1. Force the XTAL1 and XTAL2 pins low by writing 0s to the port latch.
//Step 2. Configure XTAL1 and XTAL2 as analog inputs.
//Step 3. Enable the external oscillator.
//Step 4. Wait at least 1 ms.
//Step 5. Poll for XTLVLD => '1'.
//Step 6. Switch the system clock to the external oscillator.
//------------------------------------------------------------------------------------
//
// FILE NAME : main.c
// TARGET DEVICE : C8051F314
// CREATED ON : 07/14/06
// CREATED BY : Jzp
//
// Revision 1.0
//
//------------------------------------------------------------------------------------
#include <C8051F310.H>
#include <stdio.h>
#include "..\\inc\\define.h"
#include "..\\inc\\key_scan.h"
#include "..\\inc\\ow_comm.h"
//#define SMB_MASTER 1
//#define SMB_SLAVE 0
// sfr定义
//sfr16 TMR3RL = 0x92; // Timer2 reload registers
//sfr16 TMR3 = 0x94; // Timer3 counter registers
//sfr16 TMR0 = 0X8A; // Timer0
//sbit KbBlCtl = P3^2; //键盘有动作,键盘灯点亮,否则经过延时后,熄灭
UCHAR Tmr0High;
UCHAR Tmr0Low;
UCHAR *ptr_snd;
UCHAR gSndLen;
UCHAR gSndFlag;
UCHAR xdata gSndBuf[16];
//UCHAR xdata gOwdevNum;
extern UCHAR KbIsValid;
extern UCHAR ow_reset(void);
extern void delay_us(UINT us);
//extern void ow_test(void);
//extern KbBlCtl;
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void Oscillator_Init()
{
OSCICN = 0x83; // 复位后,默认为内部振荡器。
// bit7:=1 使能内部振荡器 bit1:0 = 0x11 系统时钟为24.5M
CKCON = 0X00; // 所有的定时器都取(SCACS1:0 = 00)1/12为时钟源
}
void Timer0_Init()
{
// TMR2CN = 0x00; // Timer0工作在16位重装模式
// TMR2 = -(SYSCLK/12/100); // Timer0溢出率为10ms
// TMR2RL = -(SYSCLK/12/100);
//// TL0 = -(SYSCLK/12/100);
// TR2 = 1; // Timer0运行
TMOD |= 0X01;
TL0 = 0X40;
TH0 = 0XB0;
Tmr0High = 0xb0;
Tmr0Low = 0x40;
TR0 = 1;
}
/************************************************************
-----IIC设备初始化程序-----
入口参数:flag = 1则初始化为从机 flag = 0则初始化为主机,缺省方式为从机
只有当需要回复数据时,才将它设为主机
//void iic_init(void)
*************************************************************/
void iic_init(void)
{
SMB0CF = 0x1C; //|ENSMB|INH|BUSY|EXTHOLD|SMBTOE|SMBFTE|SMBCS1|SMBCS0
//00011100
// timer 0 溢出作为时钟源 CS = 0:0
// 因为工作与从方式,总线速度有主机决定,所以该设置对从机无意义
// 允许总线工作于从模式 INH = 0
// 允许建立/保持时间扩展 EXTHOLD = 1
// 禁止总线空闲超时检测 SMBFTE = 0
// 允许总线低电平超时检测 SMBTOE = 1
SMB0CF |= 0x80; // 总线允许;
}
/************************************************************
-----TIMER1初始化程序-----
Timer1 溢出作为UART0波特率发生器
//void Timer1_Init(void)
*************************************************************/
void UART0_Init (void)
{
// CKCON &= ~0x13; // bit1:0 = 00 SYSCLK / 12作为timer 1的时钟源
TMOD |= 0x20; // Timer 1工作在8位重装模式
TH1 = -(SYSCLK/BAUD_RATE/12/2); // Timer1溢出率为波特率的2倍
TL1 = -(SYSCLK/BAUD_RATE/12/2);
TR1 = 1; // Timer1 enabled
TI0 = 0;
RI0 = 0;
SCON0 = 0x10; // bit7 = 0:8bit 方式,bit4 = 1:允许接收
}
/************************************************************
-----TIMER3初始化程序-----
Timer3配置如下:
工作在16bit自动重装模式
sysclk/12作为它的时钟源
溢出率设置为25ms,即检测到SCL为低电平超过25ms则产生中断
//void Timer3_Init(void)
*************************************************************/
void Timer3_Init (void)
{
TMR3CN = 0x00; // 工作在16bit自动重装模式
TMR3 = -(SYSCLK/12/40); // 溢出率设置为25ms,即检测到SCL为低电平超过25ms则产生中断
TMR3RL = -(SYSCLK/12/40); //
CKCON &= ~0x40; // 复位后缺省为sysclk/12作为它的时钟源
TMR3CN |= 0x04; // Timer3 left disabled. This line should be uncommented if using SCL low timeout detection
}
void Port_IO_Init()
{
// P0.0 - SDA (SMBus), Open-Drain, Digital
// P0.1 - SCL (SMBus), Open-Drain, Digital
// P0.4 - TX0 (UART0), Open-Drain, Digital
// P0.5 - RX0 (UART0), Open-Drain, Digital
P0MDOUT = 0xC0;
P1MDOUT = 0xF0;
P2MDOUT = 0xFF;
P3MDOUT = 0x1D;
P0SKIP = 0xC0;
XBR0 = 0x05;
XBR1 = 0x40;
}
void Interrupts_Init()
{
EIE1 = 0x81; // 使能定时器3中断,使能SMBUS中断
IE = 0x12; // 使能串行中断,注意此时全局中断仍未打开
EA = 1; // 此处使能所有中断
EA = 1;
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
Oscillator_Init();
Port_IO_Init();
Timer0_Init();
UART0_Init();
Timer3_Init();
iic_init();
Interrupts_Init();
}
void main(void)
{
UCHAR i,j;
static UCHAR sndbat_flag = 1; //debug
PCA0MD &= ~0x40; // WDTE = 0 (Disable watchdog timer)
// debug one-wire protocol
// gSndFlag = 1;
// Init_Device();
// KbBlCtl = 0;
//// while(1)
//// {
// ow_test();
// delay_us(1000);
//// }
// // 串口发送rom addr
// for(i = 0; i < 8; i++)
// {
// gSndBuf[i] = g_testaddr.romaddr[i];
// }
// gSndLen = 8;
// gSndFlag = 0;
// ptr_snd = &gSndBuf[0];
// TI0 = 1;
// while(gSndFlag == 1);
// while(1);
// end debug one-wire protocol
for(j = 0; j <8; j++)
{
g_romaddr[0].romaddr[j] = 0; //将所有器件地址清零
g_romaddr[1].romaddr[j] = 0;
}
gOwdevNum = 0; //初始化无OW器件
HasDs2762 = 0; //
HasDs2415 = 0;
Init_Device(); //初始化C8051
init_keyscan();
KbBlCtl = 0;
gOwdevNum = find_ow_dev();
if(gOwdevNum != 0)
KbBlCtl = 0;
else
KbBlCtl = 1;
for(i = 0; i < 8; i++)
{
gSndBuf[i] = g_romaddr[0].romaddr[i];
}
for(i = 8; i < 16; i++)
{
gSndBuf[i] = g_romaddr[1].romaddr[i-8];
}
gSndLen = 16;
gSndFlag = 0;
ptr_snd = &gSndBuf[0];
TI0 = 1;
while(gSndFlag == 1);
// while(1);
while(1)
{
task_owcomm();
task_keyscan();
if(KbIsValid == 1)
{
for(i = 0; i < 2; i++)
{
gSndBuf[i] = KeyBuffer[i];
}
gSndLen = 2;
gSndFlag = 0;
ptr_snd = &gSndBuf[0];
TI0 = 1; //强制进入发送中断
while(gSndFlag == 1); //等待发送完成
KbIsValid = 0;
}
if(sndbat_flag == 1)
{
for(i = 0; i < 4; i++)
{
gSndBuf[i] = RtcClk[i];
}
for(i = 4; i < 6; i++)
{
gSndBuf[i] = BatVol[i-4];
}
for(i = 6; i < 8; i++)
{
gSndBuf[i] = BatCur[i-6];
}
gSndLen = 8;
gSndFlag = 0;
ptr_snd = &gSndBuf[0];
TI0 = 1; //强制进入发送中断
while(gSndFlag == 1); //等待发送完成
sndbat_flag = 0;
}
}
}
void UART0_ISR (void) interrupt 4
{
ES0 = 0;
// EA = 0;
// ptr_snd = &gSndBuf[0];
if(TI0)
{
TI0 = 0;
if(gSndLen > 0)
{
SBUF0 = *ptr_snd++;
gSndLen--;
}
else
gSndFlag = 1;
}
else if(RI0)
{
RI0 = 0;
}
else
{
TI0 = 0;
RI0 = 0;
}
ES0 = 1;
// EA = 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -