日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

stm8s跳出中断程序c语言,stm8s103 利用中断判断长按短按,长按可以实现进入低功耗 短按1-2s跳出低功耗进入工作,实际情况是短按立马跳出低功耗,请各位大侠帮忙看看。附程序...

發布時間:2023/12/31 编程问答 28 豆豆

聲明

#include ?"STM8S103F2P.h"

#define uint ?unsigned int

#define uchar unsigned char

#define NGI() ? ? ? ? ?_asm("sim"); ? ? /* 禁止全局中斷 */

#define EGI() ? ? ? ? ?_asm("rim"); ? ? /* 使能全局中斷 */

#define BitSet(a,b) ? ?(a |= (1<

#define BitClr(a,b) ? ?(a &= ~(1<

#define BitCpl(a,b) ? ?(a^=1<

#define GetBit(a,b) ? ?(((a)>>(b))&0X01)/* 獲取變量a第b位 */

stm8_interrupt.c

#include ?"lt101.h"

void ? Sun_check(void);

//void light_control(void);

typedef void @far (*interrupt_handler_t)(void);

struct interrupt_vector {

unsigned char interrupt_instruction;

interrupt_handler_t interrupt_handler;

};

@far @interrupt void NonHandledInterrupt (void)

{

/* in order to detect unexpected events during development,

it is recommended to set a breakpoint on the following instruction

*/

return;

}

@far @interrupt void PORTA_Interrupt_IRQHandler (void)

{

key_bit=1;//標志位

}

@far @interrupt void TIM4_UPD_OVF_IRQHandler (void)

{

minisecond++; ?//10ms

TIM4_SR &= 0xFE;

if((key_bit==1)&&(GetBit(PA_IDR,3))==0)//按鍵按下 并且處于低電平

{

counter++; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//counter10ms加一次,由此值判斷長按短按

}

return;

}

extern void _stext(); ? ? /* startup routine */

struct interrupt_vector const _vectab[] = {

{0x82, (interrupt_handler_t)_stext}, /* reset */

{0x82, NonHandledInterrupt}, /* trap ?*/

{0x82, NonHandledInterrupt}, /* irq0 ?*/

{0x82, NonHandledInterrupt}, /* irq1 ?*/

{0x82, NonHandledInterrupt}, /* irq2 ?*/

{0x82, PORTA_Interrupt_IRQHandler}, /* irq3 ?*/

{0x82, NonHandledInterrupt}, /* irq4 ?*/

{0x82, NonHandledInterrupt}, /* irq5 ?*/

{0x82, NonHandledInterrupt}, /* irq6 ?*/

{0x82, NonHandledInterrupt}, /* irq7 ?*/

{0x82, NonHandledInterrupt}, /* irq8 ?*/

{0x82, NonHandledInterrupt}, /* irq9 ?*/

{0x82, NonHandledInterrupt}, /* irq10 */

{0x82, NonHandledInterrupt}, /* irq11 */

{0x82, NonHandledInterrupt}, /* irq12 */

{0x82, NonHandledInterrupt}, /* irq13 */

{0x82, NonHandledInterrupt}, /* irq14 */

{0x82, NonHandledInterrupt}, /* irq15 */

{0x82, NonHandledInterrupt}, /* irq16 */

{0x82, NonHandledInterrupt}, /* irq17 */

{0x82, NonHandledInterrupt}, /* irq18 */

{0x82, NonHandledInterrupt}, /* irq19 */

{0x82, NonHandledInterrupt}, /* irq20 */

{0x82, NonHandledInterrupt}, /* irq21 */

{0x82, NonHandledInterrupt}, /* irq22 */

{0x82, TIM4_UPD_OVF_IRQHandler}, /* irq23 */

{0x82, NonHandledInterrupt}, /* irq24 */

{0x82, NonHandledInterrupt}, /* irq25 */

{0x82, NonHandledInterrupt}, /* irq26 */

{0x82, NonHandledInterrupt}, /* irq27 */

{0x82, NonHandledInterrupt}, /* irq28 */

{0x82, NonHandledInterrupt}, /* irq29 */

};

MAIN.c

/***************************************************************

**************長按進入停機模式,短按開機*********************

****************************************************************

*******/

//過放電路由硬件實現

#include ?"lt101.h"

#include ?"cpu_init.h"

void Sun_check(void); ? ? //電壓采樣

void Free_WWDG(void); ? ? //窗口看門狗

void light_control(void); //閃爍控制函數

uint ? minisecond; ? ? ? ?//light_control 函數用到的light

uint counter=0;

uchar ?sun=0;

uchar ?work_state=0; ? ? ?//等于0時工作,1時停止工作

uint ? cycle= 100; ? ? ? ?//燈亮滅周期時間1s

uchar ?lighting_time= 15; //燈亮持續時間150ms

uchar ?key_mode=0; ? ? ? ?//按鍵模式

uchar ?key_bit=0; ? ? ? ? //標志位

main()

{

NGI(); ? ? ? ?/*禁止中斷*/

CLK_Init(); ? /*時鐘初始化*/

GPIO_Init(); ?/*端口初始化*/

EXTI_Init();/*外部中斷觸發方式*/

Tim4_Init();/*定時器初始化*/

ADC_Init();

WWDG_Init(); ?/*窗口看門狗初始化*/

EGI();

while (1)

{

Free_WWDG(); ? ? ?/*喂狗*/

Sun_check(); ? ? ?/*ADC采樣*/

light_control(); ?/*燈閃爍程序*/if(counter>300) ? /*判斷按鍵按下大于3S*/

{

Free_WWDG(); ? ? ?/*喂狗*/

counter=0; ? ? ? ?/*清空計數*/

key_bit=0; ? ? ? ?/*清空標志位*/

BitClr(PC_ODR,6); /*清空按鍵*/

key_mode=0; ? ? ? /*清空按鍵*/

_asm("halt"); ? ? /*進入到低功耗模式*/

FLASH_CR1 = 0x080;

}

if((counter>100)&&(counter<200))/*若按鍵按下1-2s*/

{

Sun_check();

light_control();

}

}

}/***************************************************************

************** ADC 采樣 用來判斷白天黑夜的**********************

***************************************************************/

void Sun_check(void)

{

unsigned char i;

unsigned int AD_Value;

ADC_CSR ?= 0x05;//選擇通道AIN5

ADC_CR1 |= 0x01; ? ? ? ? /* First set ADON to power on the ADC module. ? ?*/

i = 12; ? ? ? ? ? ? ? ? ? /* Wait >7us to ensure the ADC power on finished.*/

while(i--);

ADC_CR1 |= 0x01; ? ? ? ? /* Set ADON again to start AD convert. ? ? ? ? ? */

while(!(ADC_CSR & 0x80));/* Waiting for AD convert finished (EOP=1). ? ? ?*/

AD_Value = ((((unsigned int)ADC_DRH)<<2)+ADC_DRL);

if(AD_Value > 25)

sun = 1;

if(AD_Value < 20)

sun = 0;

}

/****************************************************************

*********************燈閃爍控制**********************************

*****************************************************************

****************************************************************/

void light_control(void)

{

if(sun == 0)

{

if(minisecond <= lighting_time)

{

BitSet(PC_ODR,6);//輸出高電平,燈亮持續時間lighting_time}

if(minisecond > lighting_time)

{

BitClr(PC_ODR,6);? ?//輸出低電平,燈滅

}

if(minisecond > cycle) ? ? ? ? ?//周期時間cycle

{

Sun_check();minisecond = 0;

}

}

if(sun == 1) ? ? ? ? ? ? ? ? ? ? ? ? ?//檢測到白天

{

if(minisecond > 100)

{

BitClr(PC_ODR,6);? //輸出低電平

Sun_check();minisecond = 0;

}

}

}

/****************wwdg**************

*************窗口看門狗——喂狗程序**************

***********************************/

void Free_WWDG(void)

{

if((WWDG_CR&0X7F)<=WWDG_WR)//小于窗口值才能喂狗

WWDG_CR|=0X7F; ? //重新喂狗

}

初始化函數

#include ?"cpu_init.h"

void CLK_Init(void)

{

// CLK_ICKR = 0X08;//enable LSI

CLK_SWCR = CLK_SWCR | 0x02; ? ?// SWEN = 1

CLK_SWR = 0XD2;//chose LSI 128K

while((CLK_SWCR & 0x08) == 0); // 等待切換成功

CLK_SWCR = CLK_SWCR & 0xFD; ? ?// 清除切換

CLK_CKDIVR = 0X00; //Fmaster=128KHZ, Fcpu=128KHZ/128=1k

CLK_PCKENR1 = 0x10;//T4

CLK_PCKENR2 = 0x08;//ADC

}

void GPIO_Init(void)

{

/*PA3:KEY ?*/

BitClr(PA_DDR,3); //PA3 Input.

BitSet(PA_CR1,3); //

BitSet(PA_CR2,3); //Enable extern interrupt

/*PC3:KEY ? */

BitSet(PC_DDR,3); //PC3 Output.

BitSet(PC_CR1,3); //pull up

BitClr(PC_CR2,6); //

/*PC6

OUT*/

BitSet(PC_DDR,6); //PC6 Output.

BitSet(PC_CR1,6); //pull up

BitClr(PC_CR2,6); //

/*PD5:ADC ?*/

BitClr(PD_DDR,5); //PD5 Input. .

BitClr(PD_CR1,5); //

BitClr(PD_CR2,5); //Output speed up to 2MHz.

}

void Tim4_Init(void)

{

TIM4_IER = 0x00;

TIM4_EGR = 0x01;

TIM4_PSCR =0x03;//128k/8

TIM4_CNTR =0;

TIM4_ARR ?=160;

TIM4_CR1 = 0x01;

TIM4_SR &= 0xFE;

TIM4_IER = 0x01;

}

void ADC_Init(void)

{

ADC_CR2 ?= 0x00;

ADC_CR1 ?= 0x00;

ADC_TDRL = 0x20;

}

void WWDG_Init(void)

{

//窗口看門狗在計數值降到0x3f時產生復位,而且不能在大于窗口值時喂狗。

//否則復位

WWDG_WR=0x60; ? ? ?//看門狗窗口值,窗口值必須在0x3F以上,但必須小于計數值,否則無法喂狗

WWDG_CR=0x7F; ? ? ? //看門狗計數值

WWDG_CR|=0x80; ? ?//使能窗口看門狗

// ?128k主頻,計數值0x7F 最大延長時間為 (64 * (12288 / 128000)) = 6.14s

//看門狗

}

void EXTI_Init(void)

{

EXTI_CR1=0X02;//PA口下降沿觸發

}

總結

以上是生活随笔為你收集整理的stm8s跳出中断程序c语言,stm8s103 利用中断判断长按短按,长按可以实现进入低功耗 短按1-2s跳出低功耗进入工作,实际情况是短按立马跳出低功耗,请各位大侠帮忙看看。附程序...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。