本文整理了一些R8C常用的模块驱动,以帮助大家在使用这款单片机时快速上手。
1.时钟
初始化时钟
void sysclk_cfg(void)
{
#if SYSTEM_CLOCK==0
prc0 = 1;
prc1 = 1;
prc3 = 1;
cm02 = 0;
cm05 = 1;
cm06 = 0;
cm13 = 0;
cm15 = 1;
cm16 = 0;
cm17 = 0;
cm10 = 0;
hra00 = 1;
ocd0 = 0;
ocd1 = 0;
ocd2 = 1;
ocd3 = 0;
hra01 = 1;//Select internal 8MHz as system clock
cm14 = 1;
prc0 = 0;
#else
prc0 = 1;
prc1 = 1;
prc3 = 1;
cm02 = 0; //等等模式不停止外设时�?
cm05 = 0; //1:主时钟(外部)停�?0:使能
cm06 = 0; //0:CM16、CM17有效
cm13 = 1; //1:XIN-XOUT 0:IO
cm15 = 1; //XIN-XOUT驱动能力 1:high
//00无分�?
cm16 = 0;
cm17 = 0;
cm10 = 0; //0:时钟振荡
//振荡停止检测功能无�?
ocd0 = 0;
ocd1 = 0;
ocd2 = 0; //0:选择主时钟(外部�?1:内部时钟
cm10 = 0; //0:时钟振荡
hra01 = 1; //1:内部高�?0:内部低�?
hra00 = 0; //1:内部高速使�?
cm14 = 1; //1:内部低速晶振停�?
prc0 = 0;
#endif
}
2.定时器
定时器的初始化、复位
typedef enum{
timerX=0,
timerZ,
timerC
}TimerEnum_t;
void timerx_init(void)//定时
{
txmod0=0;
txmod1=0;
//txmr = 0x00;
txck0=1;
txck1=0;
//tcss = 0x01; // f8
r0edg=0;//INT1选为上升�?
prex = 9;
tx = 99; // (1+9)*(99+1)=1ms
txic = 0x04;
txs = 1;
}
void timer_DeInit(TimerEnum_t a)
{
switch (a)
{
case timerX:
txic = 0x00;
txs = 0;
break;
case timerZ:
tzic=0x00;
tzs=0;
default:
break;
}
}
3.UART串口
初始化串口
typedef enum{
UART_9600 = 0,
UART_14400 = 1,
UART_19200 = 2,
UART_28800 = 3,
UART_31250 =4,
UART_38400 = 5,
UART_51200 = 6,
}Uart_BaudRateTypdef;
void UART0_Init(Uart_BaudRateTypdef bps)
{
#if (SYSTEM_CLOCK==0)
unsigned char div=51;
switch(bps)
{
case (UART_9600):
{
div=51;
break;
}
case (UART_14400):
{
div=34;
break;
}
case (UART_19200):
{
div=25;
break;
}
case (UART_28800):
{
div=16;
break;
}
case (UART_31250):
{
div=15;
break;
}
case (UART_38400):
{
div=12;
break;
}
case (UART_51200):
{
div=9;
break;
}
default:
div=51;
break;
}
#else
//SYS CLOCK = 20Mhz
unsigned char div=130;
switch(bps)
{
case (UART_9600):
{
div=130;
break;
}
case (UART_14400):
{
div=87;
break;
}
case (UART_19200):
{
div=65;
break;
}
case (UART_28800):
{
div=43;
break;
}
case (UART_31250):
{
div=40;
break;
}
case (UART_38400):
{
div=32;
break;
}
case (UART_51200):
{
div=24;
break;
}
default:
div=130;
break;
}
#endif
pd1_4=1;//缁旑垰褰涢弬鐟版倻鐠佸墽鐤?
pd1_5=0;
u0brg=div;
smd0_u0mr=1;
smd1_u0mr=0;
smd2_u0mr=1;
ckdir_u0mr=0;
stps_u0mr=0;
prye_u0mr=0;
pry_u0mr=0;
/****u0c0*******/
clk0_u0c0=0;
clk1_u0c0=0;//f1
nch_u0c0=0;
ckpol_u0c0=0;
uform_u0c0=0;//0:LSB first.1:MSB first
/******UCON*****/
u0irs=0;
u0rrm=1;
cntrsel=1;
/********INT Priority**********/
s0tic=0x04;
s0ric=0x05;
/*****u0c1,Enable Tx,Rx**********/
te_u0c1=1;
re_u0c1=1;
}
4.自定义打印函数
#include "stdlib.h"//必须包含
#include
//必须包含
void UARTSendNStr(unsigned int num,unsigned char *data)
{
unsigned char *st=data;
unsigned int ptr=0;
te_u0c1=1;
while(ptr++
{
u0tb=*st;
while(!ti_u0c1);
st++;
}
}
int Println(char *format, ...)
{
va_list aptr;
int ret;
char *buffer=(char *)malloc(sizeof(char)*100);//看实际情况,不需要这么大的可以适当减少。
if(buffer == NULL)
{
//错误处理
}
va_start(aptr, format);
ret = vsprintf(buffer, format, aptr);
va_end(aptr);
UARTSendNStr(ret,buffer);
free(buffer);
return(ret);
}
5.看门狗
void init_wdt(void)
{
wdc = 0x9f; //128鍒嗛锛?
cspr = 0; //璁℃暟婧愪繚鎶ゆā寮忔棤鏁?
prc1=1;
pm12 = 1; //鐪嬮棬鐙楀浣?
prc1=0;
wdtr = 0; //鐪嬮棬鐙楀垵濮嬪寲
wdtr = 0xff;
wdts = 0; //喂狗 // watchdog 262ms
}
6.短延时
void delay_1ms(unsigned int delay_time)
{
unsigned int i;
while(delay_time !=0)
{
for (i =380; i!=0; i--)
{
asm("NOP");
asm("NOP");
}
delay_time--;
}
}
7.进入停止模式
void enter_stop(void)
{
//uint8_t i;
pm12 = 0; // watchdog由复位切换至中断
int0en = 0; //1:enable 0:disable
ir_int0ic = 0;//清中断
DI();
kien = 0x55; //
kupic = 0x06;//中断等级
fmr01 = 0; //CPU改写无效
prc0 = 1; //允许对CM0,CM1,OCD,HRA0,HRA1,HRA2寄存器写
EI();
cm10 = 1; // stop all clocks(stop mode)
asm("NOP");
asm("NOP");
asm("NOP");
asm("NOP");
asm("NOP");
asm("NOP");
DI();
sysclk_cfg();
//WDT_Clear();
pm12 = 1; // watchdog由中断切换至复位
//Int0_Init();
//Int1_Init();
kupic=0;
EI();
}
本文整理了一些R8C常用的模块驱动,以帮助大家在使用这款单片机时快速上手。
1.时钟
初始化时钟
void sysclk_cfg(void)
{
#if SYSTEM_CLOCK==0
prc0 = 1;
prc1 = 1;
prc3 = 1;
cm02 = 0;
cm05 = 1;
cm06 = 0;
cm13 = 0;
cm15 = 1;
cm16 = 0;
cm17 = 0;
cm10 = 0;
hra00 = 1;
ocd0 = 0;
ocd1 = 0;
ocd2 = 1;
ocd3 = 0;
hra01 = 1;//Select internal 8MHz as system clock
cm14 = 1;
prc0 = 0;
#else
prc0 = 1;
prc1 = 1;
prc3 = 1;
cm02 = 0; //等等模式不停止外设时�?
cm05 = 0; //1:主时钟(外部)停�?0:使能
cm06 = 0; //0:CM16、CM17有效
cm13 = 1; //1:XIN-XOUT 0:IO
cm15 = 1; //XIN-XOUT驱动能力 1:high
//00无分�?
cm16 = 0;
cm17 = 0;
cm10 = 0; //0:时钟振荡
//振荡停止检测功能无�?
ocd0 = 0;
ocd1 = 0;
ocd2 = 0; //0:选择主时钟(外部�?1:内部时钟
cm10 = 0; //0:时钟振荡
hra01 = 1; //1:内部高�?0:内部低�?
hra00 = 0; //1:内部高速使�?
cm14 = 1; //1:内部低速晶振停�?
prc0 = 0;
#endif
}
2.定时器
定时器的初始化、复位
typedef enum{
timerX=0,
timerZ,
timerC
}TimerEnum_t;
void timerx_init(void)//定时
{
txmod0=0;
txmod1=0;
//txmr = 0x00;
txck0=1;
txck1=0;
//tcss = 0x01; // f8
r0edg=0;//INT1选为上升�?
prex = 9;
tx = 99; // (1+9)*(99+1)=1ms
txic = 0x04;
txs = 1;
}
void timer_DeInit(TimerEnum_t a)
{
switch (a)
{
case timerX:
txic = 0x00;
txs = 0;
break;
case timerZ:
tzic=0x00;
tzs=0;
default:
break;
}
}
3.UART串口
初始化串口
typedef enum{
UART_9600 = 0,
UART_14400 = 1,
UART_19200 = 2,
UART_28800 = 3,
UART_31250 =4,
UART_38400 = 5,
UART_51200 = 6,
}Uart_BaudRateTypdef;
void UART0_Init(Uart_BaudRateTypdef bps)
{
#if (SYSTEM_CLOCK==0)
unsigned char div=51;
switch(bps)
{
case (UART_9600):
{
div=51;
break;
}
case (UART_14400):
{
div=34;
break;
}
case (UART_19200):
{
div=25;
break;
}
case (UART_28800):
{
div=16;
break;
}
case (UART_31250):
{
div=15;
break;
}
case (UART_38400):
{
div=12;
break;
}
case (UART_51200):
{
div=9;
break;
}
default:
div=51;
break;
}
#else
//SYS CLOCK = 20Mhz
unsigned char div=130;
switch(bps)
{
case (UART_9600):
{
div=130;
break;
}
case (UART_14400):
{
div=87;
break;
}
case (UART_19200):
{
div=65;
break;
}
case (UART_28800):
{
div=43;
break;
}
case (UART_31250):
{
div=40;
break;
}
case (UART_38400):
{
div=32;
break;
}
case (UART_51200):
{
div=24;
break;
}
default:
div=130;
break;
}
#endif
pd1_4=1;//缁旑垰褰涢弬鐟版倻鐠佸墽鐤?
pd1_5=0;
u0brg=div;
smd0_u0mr=1;
smd1_u0mr=0;
smd2_u0mr=1;
ckdir_u0mr=0;
stps_u0mr=0;
prye_u0mr=0;
pry_u0mr=0;
/****u0c0*******/
clk0_u0c0=0;
clk1_u0c0=0;//f1
nch_u0c0=0;
ckpol_u0c0=0;
uform_u0c0=0;//0:LSB first.1:MSB first
/******UCON*****/
u0irs=0;
u0rrm=1;
cntrsel=1;
/********INT Priority**********/
s0tic=0x04;
s0ric=0x05;
/*****u0c1,Enable Tx,Rx**********/
te_u0c1=1;
re_u0c1=1;
}
4.自定义打印函数
#include "stdlib.h"//必须包含
#include //必须包含
void UARTSendNStr(unsigned int num,unsigned char *data)
{
unsigned char *st=data;
unsigned int ptr=0;
te_u0c1=1;
while(ptr++
{
u0tb=*st;
while(!ti_u0c1);
st++;
}
}
int Println(char *format, ...)
{
va_list aptr;
int ret;
char *buffer=(char *)malloc(sizeof(char)*100);//看实际情况,不需要这么大的可以适当减少。
if(buffer == NULL)
{
//错误处理
}
va_start(aptr, format);
ret = vsprintf(buffer, format, aptr);
va_end(aptr);
UARTSendNStr(ret,buffer);
free(buffer);
return(ret);
}
5.看门狗
void init_wdt(void)
{
wdc = 0x9f; //128鍒嗛锛?
cspr = 0; //璁℃暟婧愪繚鎶ゆā寮忔棤鏁?
prc1=1;
pm12 = 1; //鐪嬮棬鐙楀浣?
prc1=0;
wdtr = 0; //鐪嬮棬鐙楀垵濮嬪寲
wdtr = 0xff;
wdts = 0; //喂狗 // watchdog 262ms
}
6.短延时
void delay_1ms(unsigned int delay_time)
{
unsigned int i;
while(delay_time !=0)
{
for (i =380; i!=0; i--)
{
asm("NOP");
asm("NOP");
}
delay_time--;
}
}
7.进入停止模式
void enter_stop(void)
{
//uint8_t i;
pm12 = 0; // watchdog由复位切换至中断
int0en = 0; //1:enable 0:disable
ir_int0ic = 0;//清中断
DI();
kien = 0x55; //
kupic = 0x06;//中断等级
fmr01 = 0; //CPU改写无效
prc0 = 1; //允许对CM0,CM1,OCD,HRA0,HRA1,HRA2寄存器写
EI();
cm10 = 1; // stop all clocks(stop mode)
asm("NOP");
asm("NOP");
asm("NOP");
asm("NOP");
asm("NOP");
asm("NOP");
DI();
sysclk_cfg();
//WDT_Clear();
pm12 = 1; // watchdog由中断切换至复位
//Int0_Init();
//Int1_Init();
kupic=0;
EI();
}
举报