这是我第一次看元器件的数据手册,按数据手册上的时序图,一步步写出来的驱动程序,搞了我2天时间了。我用的主芯片是LM3S9B92,时钟模块是BQ3287,(这驱动也可以驱动DS12C887 RTC模块)!
#include<inc/lm3s9b92.h>
#include"HC204Llcd.h"
#include"Sys_init.h"
#include"define_init.h"
#include"BQ3287.h"
static void Delay_1um(void)
{
asm("nop");
}
static void BQ3287_Delay(unint delayTime)
{
unsigned int i=0;
for(i = 0; i < delayTime; i++)
{
Delay_1um();
}
}
static void BQ3287_checkbusy(void)
{
unchar temp;
GPIOPinWrite(GPIO_PORTB_BASE, CS, 0x00);
GPIOPinWrite(GPIO_PORTD_BASE, RD, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, WD, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, AS, 0xFF);
Delay_1um();
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ); //使能J口
GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE,0xFF); //定义J口为输出
GPIOPinWrite(GPIO_PORTJ_BASE,0xFF,ds_reg_a);
BQ3287_Delay(10);
GPIOPinWrite(GPIO_PORTD_BASE, AS, 0x00);
GPIOPinWrite(GPIO_PORTD_BASE, RD, 0x00);
Delay_1um();
do
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ); //
GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE,ds_reg_a); //
temp=GPIOPinRead(GPIO_PORTJ_BASE,0xFF);
GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE,0xFF); //
BQ3287_Delay(10);
}
while(0x80 == (temp & 0x80));
Delay_1um();
GPIOPinWrite(GPIO_PORTB_BASE, CS, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, AS, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, RD, 0xFF);
Delay_1um();
}
void BQ3287_writecmd(unchar ds_data,unchar data)
{
BQ3287_checkbusy();
Delay_1um();
GPIOPinWrite(GPIO_PORTB_BASE, CS, 0x00);
GPIOPinWrite(GPIO_PORTD_BASE, RD, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, WD, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, AS, 0xFF);
Delay_1um();
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ); //
GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE,0xFF); //
GPIOPinWrite(GPIO_PORTJ_BASE,0xFF,ds_data);
Delay_1um();
GPIOPinWrite(GPIO_PORTD_BASE, AS, 0x00);
GPIOPinWrite(GPIO_PORTD_BASE, WD, 0x00);
GPIOPinWrite(GPIO_PORTJ_BASE,0xFF,data);
BQ3287_Delay(10);
GPIOPinWrite(GPIO_PORTB_BASE, CS, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, AS, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, WD, 0xFF);
Delay_1um();
}
unchar BQ3287_gettime(unchar ds_data)
{
unchar temp;
GPIOPinWrite(GPIO_PORTB_BASE, CS, 0x00);
GPIOPinWrite(GPIO_PORTD_BASE, RD, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, WD, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, AS, 0xFF);
Delay_1um();
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ); //
GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE,0xFF); //
GPIOPinWrite(GPIO_PORTJ_BASE,0xFF,ds_data);
BQ3287_Delay(10);
GPIOPinWrite(GPIO_PORTD_BASE, AS, 0x00);
GPIOPinWrite(GPIO_PORTD_BASE, RD, 0x00);
Delay_1um();
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ); //
GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE,0xFF); //
temp=GPIOPinRead(GPIO_PORTJ_BASE,0xFF);
BQ3287_Delay(10);
GPIOPinWrite(GPIO_PORTB_BASE, CS, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, AS, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, RD, 0xFF);
Delay_1um();
return temp;
}
void Init_BQ3287(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, WD|RD|AS);
GPIOPinWrite(GPIO_PORTD_BASE, WD, 0x00);
GPIOPinWrite(GPIO_PORTD_BASE, RD, 0x00);
GPIOPinWrite(GPIO_PORTD_BASE, AS, 0x00);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, CS);//CS
BQ3287_writecmd(0x0a,0x20); //打开振荡器
BQ3287_writecmd(ds_reg_b,0xa2);
BQ3287_writecmd(ds_sec_warn,0x00);
BQ3287_writecmd(ds_min_warn,0xee);
BQ3287_writecmd(ds_hour_warn,0xee);
BQ3287_writecmd(ds_reg_c,0x00); //清状态寄存器
BQ3287_writecmd(ds_reg_b,0x22); //禁止方波输出,24小时制,BCD码
BQ3287_Delay(5);
BQ3287_writecmd(ds_reg_b,0x22);
BQ3287_Delay(5);
}