R7F0C802X 程序试用和心得先把上篇文章的例子重上一次:
程序主要是由几部分构成,可以参照上篇文章里的r01uh0400cj0200_r7f0c801_5.pdf硬件手册对照看:
Startup.s 启动文件(C编程一般都要)
main.c 主程序,带中断子程序等都在这里
optionbyte.asm 配置文件 common.h 通用定义等。
配置字设置 Set Option Bytes 00c0~00c3四字节
00C0与看门狗有关:
4:WDTON 3:WDCS2 2: WDCS1 1:WDCS0 这四位 看门狗定时器的间隔中断时间 0:WDSTBYON看门狗定时器的计数器运行控制
00C1:
4: PORTSELB 复位脚选择 3: SPORS1 2:SPORS0 1 SPOR检测电压的设定 (中断&复位模式)
00C2: 2:FRQSEL2 1:FRQSEL1 0:FRQSEL0选择高速内部振荡器时钟的频率
000C3 :7:OCDENSET 0禁止,1允许 片上调试运行的控制
具体见r01uh0400cj0200_r7f0c801_5.pd第十八章
main.c:
IO 初始化:直接对寄存器操作:可以参考r01uh0400cj0200_r7f0c801_5.pdf硬件手册第四章
void main(void)
{
unsigned char mode;
cnt[0] = 0;
cnt[1] = 0;
cnt[2] = 0;
breath = 0;
breath_dir = 0;
/* I/O Port Init */
IO_Init();
/* TAU0 Timer Init */
TAU0_PWM_Init();
/* Enable Interrupt */
EI();
/* Start Timer, i.e. PWM output */
TAU0_Start();
mode = 1;
/* Program Loop */
while(1)
{
if (P13.7 == 0) //如果按键按下就停止闪动
{
if (mode)
{
mode = 0;
TAU0_Stop();
}
}
else //否则就启动定时器 也就LED闪动
{
if (mode == 0)
{
mode = 1;
TAU0_Start();
}
}
} /* while(1) */
}
//这里涉及到了所有端口寄存器,如果IO做其他用途可以适当修改
void IO_Init(void)
{
/* Port Mode '0'=Out, '1'=In */
PM0 = 0x01;
PM4 = 0x00;
/* Port Register */
P0 = 0x00;
P4 = 0x00;
/* Pull-up resistor */
PU0 = 0x00;
PU4 = 0x00;
PU12 = 0x00;
/* Port output mode '1'=open-drain */
POM0 = 0x03;
/* Port mode control '0'=I/O, '1'=AIN*/
PMC0 = 0xE1;
/* Peripheral I/O redirection */
PIOR = 0x00;
}
各16 位定时器称为 “ 通道 ”,既能分别用作独立的定时器,也能组合多个通道用作高级的定时器功能。有四个
主要参考r01uh0400cj0200_r7f0c801_5.pdf硬件手册第六,七章
定时器开启
/*
**---------------------------------------------------------------------
** Abstract: This function start the timer.
** Parameters: None
** Returns: None
**---------------------------------------------------------------------
*/
void TAU0_Start(void)
{
TMIF00 = 0U; /* clear INTTM00 interrupt flag */
TMMK00 = 0U; /* enable INTTM00 interrupt */
TMIF01 = 0U; /* clear INTTM01 interrupt flag */
TMMK01 = 0U; /* enable INTTM01 interrupt */
TOE0 |= 0x03U; /* enable CH1 output */
TS0 = 0x03U; /* Trigger CH0 start */
}
定时器停止
/*
**---------------------------------------------------------------------
** Abstract: This function stop the timer.
** Parameters: None
** Returns: None
**---------------------------------------------------------------------
*/
void TAU0_Stop(void)
{
TT0 |= 0x03; /* Trigger CH0, CH1 to stop */
TOE0 &= ~0x02; /* disable CH1 output */
TMMK00 = 1U; /* disable INTTM00 interrupt */
TMIF00 = 0U; /* clear INTTM00 interrupt flag */
TMMK01 = 1U; /* disable INTTM01 interrupt */
TMIF01 = 0U; /* clear INTTM01 interrupt flag */
}
PWM初始化
/*
**---------------------------------------------------------------------
** Abstract : This function implements timer initialization for PWM output.
** Parameters : None
** Returns : None
** Remark : Set PWM frequency = 1KHz,
**---------------------------------------------------------------------
*/
void TAU0_PWM_Init(void)
{
TAU0EN = 1; /* supplies input clock */
TPS0 = 0x0F; /* CK01=fclk, CK00=fclk/2^15 */
/* Stop all channels */
TT0 = 0x00U;
/* Mask channel 0 interrupt */
TMMK00 = 1U; /* disable INTTM00 interrupt */
TMIF00 = 0U; /* clear INTTM00 interrupt flag */
/* Mask channel 1 interrupt */
TMMK01 = 1U; /* disable INTTM01 interrupt */
TMIF01 = 0U; /* clear INTTM01 interrupt flag */
/* Mask channel 1 higher 8 bits interrupt */
TMMK01H = 1U; /* disable INTTM01H interrupt */
TMIF01H = 0U; /* clear INTTM01H interrupt flag */
/* Set INTTM00 low priority */
TMPR100 = 1U;
TMPR000 = 1U;
/* Channel 0 is used as master channel for PWM output function */
TMR00H = 0x80U; /* CK01 */
TMR00L = 0x01U;
#if 0
TDR00H = 0x13U; /* TDR00 = 4999+1 (0x1387H+1), 1KHz */
TDR00L = 0x87U;
#else
TDR00H = 0x1FU; /* TDR00 = 8191+1 (0x1fffH+1), ~610Hz */
TDR00L = 0xFFU;
#endif
TOL0 &= ~0x01;
TO0 &= ~0x01;
TOE0 |= ~0x01;
/* Channel 1 is used as slave channel for PWM output function */
TMR01H = 0x84U; /* CK01 */
TMR01L = 0x09U;
#if 0
TDR01H = 0x03U; /* TDR01 set duty ratio of TDR00 */
TDR01L = 0xE7U; /* TDR01 = 999+1 (0x03E7+1), 20% Duty ratio */
#else
TDR01H = 0x00U;
TDR01L = 0x00U;
#endif
TOM0 = 0x02U;
TO0 &= ~0x02;
TO0 |= 0x02U;
TOE0 |= 0x02U;
TOL0 = 0x02U;
/* Set TO00, TO01 pin */
P0 &= 0xE7U;
PM0 &= 0xE7U;
}
中断响应参考r01uh0400cj0200_r7f0c801_5.pdf硬件手册第13章
定时中断响应00程序
/*
**---------------------------------------------------------------------
** Abstract: This function implements timer interrupt.
** Parameters: None
** Returns: None
**---------------------------------------------------------------------
*/
__interrupt void interrupt_inttm00(){
cnt[0]++;
if (breath_dir == 0)
{
/* dimm on */
if (breath <= 0x1fff)
{
breath += 0x10;
}
else
{
breath = 0x2000;
breath_dir = 1;
}
}
else
{
/* dimm off */
if (breath > 0)
{
breath -= 0x100;
}
else
{
breath = 0;
breath_dir = 0;
}
}
TDR01H = (unsigned char)(breath>>8);
TDR01L = (unsigned char)(breath&0x00ff);
}
定时中断响应01程序
/*
**---------------------------------------------------------------------
** Abstract: This function implements timer interrupt.
** Parameters: None
** Returns: None
**---------------------------------------------------------------------
*/
__interrupt void interrupt_inttm01(){
cnt[1]++;
}
定时中断响应01h程序?
/*
**---------------------------------------------------------------------
** Abstract: This function implements timer interrupt.
** Parameters: None
** Returns: None
**---------------------------------------------------------------------
*/
__interrupt void interrupt_inttm01h(){
cnt[2]++;
}
本帖最后由 damiaa 于 2014-10-19 17:22 编辑