stm8l051 遥控器的程序大家可以参考参考 我写了1星期才写出来的 没用定时器全部延时,通过验证样品已经实验成功!!!
加低功耗加看门狗 待机2uA 最大工作电流1.6mA。
/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"
#include "stm8l15x_it.h"
#include "delay.h"
void SendIRdata(unsigned char p_irdata);
uint8_t hwx[14]={0X00,0x01,0x02,0x03,0x04,0x05,0x06,0x0d,0x08,0x05,0x11,0x0b,0x0c,0x0d};
#define RELOAD_VALUE 254
u8 qq,aa1,bt1;
u8 g_OP;
u16 g_count; //延时计数器
u16 g_endcount; //终止延时计数
u8 g_flag; //红外发送标志
u8 g_iraddr1=0; //十六位地址的第一个字节
u8 g_iraddr2=191; //十六位地址的第二个字节
extern uint32_t hw[13];
void CLK_Config(void)
{
/* Select HSE as system clock source */
// CLK_SYSCLKSourceSwitchCmd(ENABLE);//使能时钟切换
// CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);//选择内部低速时钟作为时钟源
/* system clock prescaler: 1*/
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_2);//设置系统时钟分频
// while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_HSI)//等待时钟稳定
// {}
// CLK_HSICmd(DISABLE); //关闭原先的 CLK source
// CLK_HSEConfig(CLK_HSE_OFF); //关闭外部 CLK source
}
void SendIRdata_38KHZ(u16 temp1, u8 temp2)
{
g_endcount=temp1;
g_flag=temp2;
IWDG_ReloadCounter();
while(g_endcount--)
{
// g_count++;
if(g_flag) {delay_13us11();}
else {delay_13us1();}
}
}
/////////////////////////////////////////////////////
void SendIRdata_BYTE(unsigned char irdata)
{
unsigned char i;
for(i=0;i<8;i++)
{
// delay_13us1();
//delay_13us11();
//先发送0.56ms的38KHZ红外波(即编码中0.56ms的高电平)
SendIRdata_38KHZ(43, 1); //13.02*43=0.56ms
//停止发送红外信号(即编码中的低电平)
if(irdata & 1) //判断最低位为1还是0。 低位先发送!!
SendIRdata_38KHZ(130, 0); //1为宽电平,13.02*130=1.693ms
else SendIRdata_38KHZ(43, 0); //0为窄电平,13.02*43=0.560ms
irdata=irdata>>1;
}
}
void Sendlianfqa(void)
{
//发送9ms的起始码,高电平有38KHZ载波
SendIRdata_38KHZ(692, 1); //13.02*692=9.010ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);
SendIRdata_38KHZ(346, 0); //13.02*346=4.505ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);
SendIRdata_38KHZ(43, 0); //13.02*692=9.010ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);//65535
SendIRdata_38KHZ(6923, 0); //13.02*1766=90ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);
}
/////////////////////////////////////////////////////
void SendIRdata(unsigned char p_irdata)
{
//有的遥控器会发一个前脉冲,如果不灵,可试试加上前脉冲
//发送起始码前脉冲,高电平有38KHZ载波
//SendIRdata_38KHZ(18, 1);
//发送起始码前脉冲,低电平无38KHZ载波
//SendIRdata_38KHZ(18, 0);
//发送9ms的起始码,高电平有38KHZ载波
SendIRdata_38KHZ(692, 1); //13.02*692=9.010ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);
//发送4.5ms的结果码,低电平无38KHZ载波
SendIRdata_38KHZ(346, 0); //13.02*346=4.505ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);
//发送十六位地址的前八位
SendIRdata_BYTE(g_iraddr1);
//发送十六位地址的后八位
SendIRdata_BYTE(g_iraddr2);
GPIO_SetBits(GPIOC, GPIO_Pin_6);
//发送八位数据
SendIRdata_BYTE(p_irdata);
GPIO_SetBits(GPIOC, GPIO_Pin_6);
//发送八位数据的反码
SendIRdata_BYTE(~p_irdata);
GPIO_SetBits(GPIOC, GPIO_Pin_6);
//发送总的结束位1bit
SendIRdata_38KHZ(43, 1); //13.02*43=0.56ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);
SendIRdata_38KHZ(2400, 0); //13.02*2400=30ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);
/* //后面这些可以不用发
SendIRdata_38KHZ(1766, 0); //13.02*1766=23ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);
//发送9ms的起始码,高电平有38KHZ载波
SendIRdata_38KHZ(692, 1); //13.02*692=9.010ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);
SendIRdata_38KHZ(43, 0); //13.02*692=9.010ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);
//发送4.5ms的结果码,低电平无38KHZ载波
SendIRdata_38KHZ(346, 0); //13.02*346=4.505ms
GPIO_SetBits(GPIOC, GPIO_Pin_6);
g_endcount=1766;
g_flag=0;
EA=0; g_count=0; EA=1;
while(1){EA=0; if(g_count<g_endcount) EA=1; else { EA=1; break; } }
//发送9ms的起始码,高电平有38KHZ载波
g_endcount=692; //13.02*692=9.010ms
g_flag=1;
EA=0; g_count=0; EA=1;
while(1){EA=0; if(g_count<g_endcount) EA=1; else { EA=1; break; } }
//发送4.5ms的结果码,低电平无38KHZ载波
g_endcount=346; //13.02*346=4.505ms
g_flag=0;
EA=0; g_count=0; EA=1;
while(1){EA=0; if(g_count<g_endcount) EA=1; else { EA=1; break; } }
//发送总的结束位1bit
g_endcount=43; //13.02*43=0.56ms
g_flag=1;
EA=0; g_count=0; EA=1;
while(1){EA=0; if(g_count<g_endcount) EA=1; else { EA=1; break; } }
*/
g_flag=0;
IWDG_ReloadCounter();
}
/**
* @brief configure GPIOs before entering low power
* @caller Halt_Init and ADC_Icc_Test
* @param None
* @retval None
*/
void GPIO_LowPower_Config(void)
{
GPIO_Init(GPIOA, GPIO_Pin_2|GPIO_Pin_3, GPIO_Mode_In_PU_IT);
GPIO_Init(GPIOB, GPIO_Pin_All, GPIO_Mode_In_PU_IT);
GPIO_Init(GPIOC, GPIO_Pin_6, GPIO_Mode_Out_PP_High_Fast);
GPIO_Init(GPIOC, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_5, GPIO_Mode_In_PU_IT);
// GPIO_Init(GPIOD, GPIO_Pin_All, GPIO_Mode_In_FL_IT);
}
void exit_Config(void)
{
EXTI_SetPinSensitivity(EXTI_Pin_0,EXTI_Trigger_Falling_Low);
ITC_SetSoftwarePriority(EXTI0_IRQn, ITC_PriorityLevel_1);//设置中断优先级
EXTI_SetPinSensitivity(EXTI_Pin_1,EXTI_Trigger_Falling_Low);
ITC_SetSoftwarePriority(EXTI1_IRQn, ITC_PriorityLevel_1);//设置中断优先级
EXTI_SetPinSensitivity(EXTI_Pin_2,EXTI_Trigger_Falling_Low);
ITC_SetSoftwarePriority(EXTI2_IRQn, ITC_PriorityLevel_1);//设置中断优先级
EXTI_SetPinSensitivity(EXTI_Pin_3,EXTI_Trigger_Falling_Low);
ITC_SetSoftwarePriority(EXTI3_IRQn, ITC_PriorityLevel_1);//设置中断优先级
EXTI_SetPinSensitivity(EXTI_Pin_4,EXTI_Trigger_Falling_Low);
ITC_SetSoftwarePriority(EXTI4_IRQn, ITC_PriorityLevel_1);//设置中断优先级
EXTI_SetPinSensitivity(EXTI_Pin_5,EXTI_Trigger_Falling_Low);
ITC_SetSoftwarePriority(EXTI5_IRQn, ITC_PriorityLevel_1);//设置中断优先级
EXTI_SetPinSensitivity(EXTI_Pin_6,EXTI_Trigger_Falling_Low);
ITC_SetSoftwarePriority(EXTI6_IRQn, ITC_PriorityLevel_1);//设置中断优先级
EXTI_SetPinSensitivity(EXTI_Pin_7,EXTI_Trigger_Falling_Low);
ITC_SetSoftwarePriority(EXTI7_IRQn, ITC_PriorityLevel_1);//设置中断优先级
}
void watchdog_init(void)//IWDG :IWDG TIMEOUT=(RELOAD_VALUE+1)*IWDG_Prescaler/LSI=(254+1)*256/38000=1724ms
{
IWDG_Enable(); //
/* Enable write access to IWDG_PR and IWDG_RLR registers */
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
/* Set IWDG timeout */
IWDG_SetPrescaler(IWDG_Prescaler_256);
IWDG_SetReload(RELOAD_VALUE);
/* Refresh IWDG */
IWDG_ReloadCounter();
}
/**
* @brief main entry point.
* @par Parameters None
* @retval void None
* @par Required preconditions: None
*/
void main(void)
{
/*设置时钟主频*/
//CLK_Config();
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_2);//设置系统时钟分频
GPIO_LowPower_Config();
enableInterrupts(); /* 开所有中断 */
watchdog_init();
exit_Config();
g_count=0;
g_OP = 1;
while (1)
{
IWDG_ReloadCounter();
halt();
}
}
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
一周热门 更多>