stmC語言例程_第1頁
stmC語言例程_第2頁
stmC語言例程_第3頁
stmC語言例程_第4頁
stmC語言例程_第5頁
已閱讀5頁,還剩19頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、(1);名稱 :流水燈;描述 : ;先從上至下點(diǎn)亮所有的LED,再逐個點(diǎn)亮單個LED;*/#include "stm8s105s4.h"#define uchar unsigned char#define uint unsigned intuchar table=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f; /存放點(diǎn)亮單個LED的代碼void DelayMS(uint s) /延時子函數(shù),約延時1ms uint i;for(s;s>0;s-) for(i=0;i<500;i+); void init(void) /stm8初始

2、化函數(shù)PB_DDR = 0xff;PB_CR1 = 0xff; / 將PB設(shè)置成推挽輸出PB_CR2 = 0x00;CLK_SWR = 0xE1; /選內(nèi)部高速時鐘作為主時鐘CLK_CKDIVR = 0x08; /將CPU主頻設(shè)置為2M(STM8默認(rèn)的就是內(nèi)部高速時鐘的八分頻,即2MH,這里只是告訴大家設(shè)置方法)void main()uchar i;init();while (1) /無限循環(huán)PB_ODR = 0xff; /先將所有的LED關(guān)閉for(i=0;i<9;i+) /一開始是所有的LED熄滅,再逐點(diǎn)亮所有LED,共九種狀態(tài)DelayMS(500); /延時500毫秒PB_ODR

3、<<=1; /將PB_ODR向左移動一位,逐漸點(diǎn)亮所有LED for(i=0;i<8;i+)PB_ODR=tablei; /將table中的數(shù)依次賦給PB_ODR ,從上至下依次點(diǎn)亮LEDDelayMS(500); 中斷向量:/*BASIC INTERRUPT VECTOR TABLE FOR STM8 devices *Copyright (c) 2007 STMicroelectronics */typedef void far (*interrupt_handler_t)(void);struct interrupt_vector unsigned char inter

4、rupt_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;extern void _stext(); /* startup routine */struct interrupt_vecto

5、r const _vectab = 0x82, (interrupt_handler_t)_stext, /* reset */0x82, NonHandledInterrupt, /* trap */0x82, NonHandledInterrupt, /* irq0 */0x82, NonHandledInterrupt, /* irq1 */0x82, NonHandledInterrupt, /* irq2 */0x82, NonHandledInterrupt, /* irq3 */0x82, NonHandledInterrupt, /* irq4 */0x82, NonHandl

6、edInterrupt, /* irq5 */0x82, NonHandledInterrupt, /* irq6 */0x82, NonHandledInterrupt, /* irq7 */0x82, NonHandledInterrupt, /* irq8 */0x82, NonHandledInterrupt, /* irq9 */0x82, NonHandledInterrupt, /* irq10 */0x82, NonHandledInterrupt, /* irq11 */0x82, NonHandledInterrupt, /* irq12 */0x82, NonHandle

7、dInterrupt, /* irq13 */0x82, NonHandledInterrupt, /* irq14 */0x82, NonHandledInterrupt, /* irq15 */0x82, NonHandledInterrupt, /* irq16 */0x82, NonHandledInterrupt, /* irq17 */0x82, NonHandledInterrupt, /* irq18 */0x82, NonHandledInterrupt, /* irq19 */0x82, NonHandledInterrupt, /* irq20 */0x82, NonHa

8、ndledInterrupt, /* irq21 */0x82, NonHandledInterrupt, /* irq22 */0x82, NonHandledInterrupt, /* irq23 */0x82, NonHandledInterrupt, /* irq24 */0x82, NonHandledInterrupt, /* irq25 */0x82, NonHandledInterrupt, /* irq26 */0x82, NonHandledInterrupt, /* irq27 */0x82, NonHandledInterrupt, /* irq28 */0x82, N

9、onHandledInterrupt, /* irq29 */;(2):矩陣鍵盤、數(shù)碼管:;描述 : 按下相應(yīng)按鍵顯示0到F中對應(yīng)的數(shù);*/#include <stm8s105s4.h>#define uint unsigned int #define uchar unsigned char uchar table=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;uchar num;void delay(uchar a)uchar i;for(a;a>0;a-) f

10、or(i=0;i<255;i+);void init(void)PB_DDR=0XFF; PB_CR1=0XFF; / 將PB設(shè)置成推挽輸出 PC_DDR=0X1F; PC_CR1=0XFF; / 將PC設(shè)置成推挽輸出 PE_DDR=0x26; PE_CR1=0XFF; /將PE5,PE2,PE1為推挽輸出,其他的設(shè)為上拉輸入 PD_DDR=0X80; PD_CR1=0X80; /將PD7設(shè)為推挽輸出/*這里的設(shè)置比LED的和數(shù)碼管的設(shè)置要復(fù)雜,開始引入輸入 模式,矩陣鍵盤列為輸出,行為輸入,請參考協(xié)會開發(fā)板原理圖及STM8轉(zhuǎn)51PDF資料*/*掃描方式也可為行輸出,列輸入,讀者可以自己

11、嘗試著修改*/uchar keys(void) /此子函數(shù)看起來很長,實(shí)則只有前面一截內(nèi)容,下面的“同理可得”uchar temp;PE_ODR=0X26; /將列中的PD7置為低電平,列中的其他三個置為高電平PD_ODR=0x00;delay(40); /這里要說明一下,由于矩陣鍵盤第一列和AD鍵盤共用一個IO口/AD鍵盤的RC會影響IO口電平變化的時間,這里需要延時一段時間,讓電容C放電完畢if(PE_IDR&0x40)=0) /如果第一列的第四行按鍵被按下,則進(jìn)入if語句內(nèi) delay(5); /延時一段時間,等待IO口電平穩(wěn)定,即消抖if(PE_IDR&0x40)=0)

12、 /再次判斷按鍵是否被按下,避免干擾 num=12; /如果第一列的第四行按鍵被按下,則令num=12,即數(shù)碼管顯示 為C while(PE_IDR&0x40)=0); /如果按鍵沒有松開,則等待 temp=PC_IDR&0xe0; if(temp!=0xe0) delay(5); temp=PC_IDR&0xe0; if(temp!=0xe0) switch(temp) case 0xc0:num=0; /如果temp的值為0xc0,則說明第1個按鍵被按下,下面的依次類推break; case 0xa0:num=4;break; case 0x60:num=8;bre

13、ak; while(PC_IDR&0xe0)!=0xe0); PE_ODR=0X24;PD_ODR=0x80;if(PE_IDR&0x40)=0) num=13;delay(5);while(PE_IDR&0x40)=0); temp=PC_IDR&0xe0;if(temp!=0xe0) delay(5); temp=PC_IDR&0xe0; if(temp!=0xe0) switch(temp) case 0xc0:num=1;break; case 0xa0:num=5;break; case 0x60:num=9;break; while(PC_I

14、DR&0xe0)!=0xe0); PE_ODR=0X22;PD_ODR=0x80;if(PE_IDR&0x40)=0) num=14;delay(5);while(PE_IDR&0x40)=0); temp=PC_IDR&0xe0;if(temp!=0xe0) delay(5); temp=PC_IDR&0xe0; if(temp!=0xe0) switch(temp) case 0xc0:num=2;break; case 0xa0:num=6;break; case 0x60:num=10;break; while(PC_IDR&0xe0)!

15、=0xe0); PE_ODR=0X06;PD_ODR=0x80;if(PE_IDR&0x40)=0) num=15;delay(5);while(PE_IDR&0x40)=0); temp=PC_IDR&0xe0;if(temp!=0xe0) delay(5); temp=PC_IDR&0xe0; if(temp!=0xe0) switch(temp) case 0xc0:num=3;break; case 0xa0:num=7;break; case 0x60:num=11;break; while(PC_IDR&0xe0)!=0xe0); retur

16、n num; void main()uchar n;init();while(1) n=keys(); /把函數(shù)keys()的返回值num賦給nPB_ODR=tablen; PC_ODR|=0x00; /選擇第一個數(shù)碼管中斷向量:/*BASIC INTERRUPT VECTOR TABLE FOR STM8 devices *Copyright (c) 2007 STMicroelectronics */typedef void far (*interrupt_handler_t)(void);struct interrupt_vector unsigned char interrupt_in

17、struction;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;extern void _stext(); /* startup routine */struct interrupt_vector const

18、 _vectab = 0x82, (interrupt_handler_t)_stext, /* reset */0x82, NonHandledInterrupt, /* trap */0x82, NonHandledInterrupt, /* irq0 */0x82, NonHandledInterrupt, /* irq1 */0x82, NonHandledInterrupt, /* irq2 */0x82, NonHandledInterrupt, /* irq3 */0x82, NonHandledInterrupt, /* irq4 */0x82, NonHandledInter

19、rupt, /* irq5 */0x82, NonHandledInterrupt, /* irq6 */0x82, NonHandledInterrupt, /* irq7 */0x82, NonHandledInterrupt, /* irq8 */0x82, NonHandledInterrupt, /* irq9 */0x82, NonHandledInterrupt, /* irq10 */0x82, NonHandledInterrupt, /* irq11 */0x82, NonHandledInterrupt, /* irq12 */0x82, NonHandledInterr

20、upt, /* irq13 */0x82, NonHandledInterrupt, /* irq14 */0x82, NonHandledInterrupt, /* irq15 */0x82, NonHandledInterrupt, /* irq16 */0x82, NonHandledInterrupt, /* irq17 */0x82, NonHandledInterrupt, /* irq18 */0x82, NonHandledInterrupt, /* irq19 */0x82, NonHandledInterrupt, /* irq20 */0x82, NonHandledIn

21、terrupt, /* irq21 */0x82, NonHandledInterrupt, /* irq22 */0x82, NonHandledInterrupt, /* irq23 */0x82, NonHandledInterrupt, /* irq24 */0x82, NonHandledInterrupt, /* irq25 */0x82, NonHandledInterrupt, /* irq26 */0x82, NonHandledInterrupt, /* irq27 */0x82, NonHandledInterrupt, /* irq28 */0x82, NonHandl

22、edInterrupt, /* irq29 */;(3);名稱 :定時器的使用 ;描述 : 數(shù)碼管顯示0F,利用定時器使得1秒變換一次;*/#include <stm8s105s4.h>#define uint unsigned int #define uchar unsigned char uchar table=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e;uchar t;void init(void) PB_DDR=0XFF; PB_CR1=0XFF; PB_CR

23、2=0X00; PC_DDR=0XFF; PC_CR1=0XFF; PC_CR2=0X00; TIM2_EGR=0X01; /允許產(chǎn)生更新事件 TIM2_PSCR=0X01; /分頻,使頻率為1MHz TIM2_ARRH=0XC3; /更新后計(jì)數(shù)器的值 TIM2_ARRL=0X50; TIM2_CR1=0X05; /允許定時器工作 TIM2_IER=0X01; /允許更新中斷 _asm("rim"); /匯編語句,啟動定時器 /注意!使用定時器時要定義中斷函數(shù)入口,詳請打開main.c下面的stm8_interrupt_vector.c,請按照注釋進(jìn)行修改 void mai

24、n(void) uchar i=0,j; init(); while(1) PB_ODR=tablei; PC_ODR=0x02; /選擇第二個數(shù)碼管顯示 if(t=20) /這里設(shè)置50ms進(jìn)入一次中斷,t=20剛好為1秒 t=0; /t清零 i+; if(i=16) i=0; /由于數(shù)組中只有16個數(shù),所以i最大只能為15,否則顯示會出現(xiàn)亂碼現(xiàn)象 far interrupt void TIM2_UP_IRQHandler (void) /中斷函數(shù) TIM2_SR1 = 0x00; /進(jìn)入中斷時TIM2_SR1最低位會被硬件自動置一,進(jìn)入中斷后必須將其清零,否則無法再次產(chǎn)生中斷 t+; /進(jìn)

25、入中斷后t自加1/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices * Copyright (c) 2007 STMicroelectronics */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 orde

26、r to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction */ return;extern void _stext(); /* startup routine */extern far interrupt void TIM2_UP_IRQHandler (void); /使用定時器要加上這句話struct interrupt_vector const _vectab = 0x82, (interrupt_handler_

27、t)_stext, /* reset */ 0x82, NonHandledInterrupt, /* trap */ 0x82, NonHandledInterrupt, /* irq0 */ 0x82, NonHandledInterrupt, /* irq1 */ 0x82, NonHandledInterrupt, /* irq2 */ 0x82, NonHandledInterrupt, /* irq3 */ 0x82, NonHandledInterrupt, /* irq4 */ 0x82, NonHandledInterrupt, /* irq5 */ 0x82, NonHan

28、dledInterrupt, /* irq6 */ 0x82, NonHandledInterrupt, /* irq7 */ 0x82, NonHandledInterrupt, /* irq8 */ 0x82, NonHandledInterrupt, /* irq9 */ 0x82, NonHandledInterrupt, /* irq10 */ 0x82, NonHandledInterrupt, /* irq11 */ 0x82, NonHandledInterrupt, /* irq12 */ 0x82, TIM2_UP_IRQHandler, /* irq13 */ /使用定時

29、器要把這里的“NonHandledInterrupt”改為“TIM2_UP_IRQHandler”,即中斷函數(shù)名 0x82, NonHandledInterrupt, /* irq14 */ 0x82, NonHandledInterrupt, /* irq15 */ 0x82, NonHandledInterrupt, /* irq16 */ 0x82, NonHandledInterrupt, /* irq17 */ 0x82, NonHandledInterrupt, /* irq18 */ 0x82, NonHandledInterrupt, /* irq19 */ 0x82, Non

30、HandledInterrupt, /* irq20 */ 0x82, NonHandledInterrupt, /* irq21 */ 0x82, NonHandledInterrupt, /* irq22 */ 0x82, NonHandledInterrupt, /* irq23 */ 0x82, NonHandledInterrupt, /* irq24 */ 0x82, NonHandledInterrupt, /* irq25 */ 0x82, NonHandledInterrupt, /* irq26 */ 0x82, NonHandledInterrupt, /* irq27

31、*/ 0x82, NonHandledInterrupt, /* irq28 */ 0x82, NonHandledInterrupt, /* irq29 */;(3) (4):蜂鳴器:定時器方式;名稱 :蜂鳴器 ;描述 : 利用定時器產(chǎn)生;*/#include <stm8s105s4.h>#define uchar unsigned char _Bool beep PD_ODR:4;uchar t2,t3;void init(void)PD_DDR=0X10;PD_CR1=0X10; /連接蜂鳴器的IO口初始化PD_CR2=0X00;PD_ODR|=0x10;TIM2_EGR=0

32、X01; /允許產(chǎn)生更新事件TIM2_PSCR=0X01; /分頻,使頻率為1MHz TIM2_ARRH=0XC3; /更新后計(jì)數(shù)器的值TIM2_ARRL=0X50; /定時50ms,用于控制發(fā)聲時間 TIM2_CR1=0X05; /允許定時器工作TIM2_IER=0X01; /允許更新中斷TIM3_EGR=0X01; /允許產(chǎn)生更新事件TIM3_PSCR=0X01; /分頻,使頻率為1MHz TIM3_ARRH=0X00; /更新后計(jì)數(shù)器的值TIM3_ARRL=0Xfa; /這里定時0.25ms,用于產(chǎn)生所需的頻率 TIM3_CR1=0X05; /允許定時器工作TIM3_IER=0X01;

33、/允許更新中斷_asm("rim"); /匯編語句,啟動定時器void delay(uchar s)uchar i;for(s;s>0;s-) for(i=0;i<250;i+);void main()init();while(1) if(t3%2=0) /前一秒產(chǎn)生2KHz頻率的聲音 beep=1; /定時器3時間0.25ms,一個周期0.5ms,故頻率為2Kelse beep=0;while(t2>=20&&t2<40) /第二秒產(chǎn)生1KHz頻率聲音 if(t3%4<2) beep=1; else beep=0;while(

34、t2>=40&&t2<60) /第三秒產(chǎn)生500Hz頻率聲音 if(t3%8<4) beep=1; elsebeep=0;if(t2>=60) t2=0; if(t3=8) t3=0;far interrupt void TIM2_UP_IRQHandler (void) /定時器2中斷函數(shù) TIM2_SR1 = 0x00; /清零TIM2_SR1 t2+;far interrupt void TIM3_UP_IRQHandler (void) /定時器三中斷函數(shù) TIM3_SR1 = 0x00; /清零TIM2_SR1 t3+;中斷向量:/*BASIC

35、 INTERRUPT VECTOR TABLE FOR STM8 devices *Copyright (c) 2007 STMicroelectronics */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 unexpec

36、ted events during development, it is recommended to set a breakpoint on the following instruction*/return;extern void _stext(); /* startup routine */extern far interrupt void TIM2_UP_IRQHandler (void);extern far interrupt void TIM3_UP_IRQHandler (void);struct interrupt_vector const _vectab = 0x82, (

37、interrupt_handler_t)_stext, /* reset */0x82, NonHandledInterrupt, /* trap */0x82, NonHandledInterrupt, /* irq0 */0x82, NonHandledInterrupt, /* irq1 */0x82, NonHandledInterrupt, /* irq2 */0x82, NonHandledInterrupt, /* irq3 */0x82, NonHandledInterrupt, /* irq4 */0x82, NonHandledInterrupt, /* irq5 */0x

38、82, NonHandledInterrupt, /* irq6 */0x82, NonHandledInterrupt, /* irq7 */0x82, NonHandledInterrupt, /* irq8 */0x82, NonHandledInterrupt, /* irq9 */0x82, NonHandledInterrupt, /* irq10 */0x82, NonHandledInterrupt, /* irq11 */0x82, NonHandledInterrupt, /* irq12 */0x82, TIM2_UP_IRQHandler, /* irq13 */0x8

39、2, NonHandledInterrupt, /* irq14 */0x82, TIM3_UP_IRQHandler, /* irq15 */0x82, NonHandledInterrupt, /* irq16 */0x82, NonHandledInterrupt, /* irq17 */0x82, NonHandledInterrupt, /* irq18 */0x82, NonHandledInterrupt, /* irq19 */0x82, NonHandledInterrupt, /* irq20 */0x82, NonHandledInterrupt, /* irq21 */

40、0x82, NonHandledInterrupt, /* irq22 */0x82, NonHandledInterrupt, /* irq23 */0x82, NonHandledInterrupt, /* irq24 */0x82, NonHandledInterrupt, /* irq25 */0x82, NonHandledInterrupt, /* irq26 */0x82, NonHandledInterrupt, /* irq27 */0x82, NonHandledInterrupt, /* irq28 */0x82, NonHandledInterrupt, /* irq2

41、9 */;(5) 蜂鳴器:stm8BEEP方式;名稱 :STM8自帶蜂鳴器使用;描述 : ;注!此功能需用ST Link輔助設(shè)置,協(xié)會實(shí)驗(yàn)板無法正常工作,這里僅作參考;*/#include <stm8s105s4.h>#define uchar unsigned charuchar t;void init(void) /初始化函數(shù) CLK_ICKR|=0x08; / 打開芯片內(nèi)部的低速振蕩器LSI while(CLK_ICKR&0x10)=0); / 等待振蕩器穩(wěn)定 TIM2_EGR=0X01; /允許產(chǎn)生更新事件 TIM2_PSCR=0X01; /分頻,使頻率為1MHz

42、TIM2_ARRH=0XC3; /更新后計(jì)數(shù)器的值 TIM2_ARRL=0X50; TIM2_CR1=0X05; /允許定時器工作 TIM2_IER=0X01; /允許更新中斷 _asm("rim"); /匯編語句,啟動定時器 void main() uchar i; init(); while(1) BEEP_CSR=0x26; /一秒2KHz while(t>=20)&&(t<40) BEEP_CSR=0x2e; /一秒1KHz while(t>=40)&&(t<60) BEEP_CSR=0x3e; /一秒500H

43、z while(t>=60)&&(t<80) BEEP_CSR&=0xdf; /一秒關(guān)閉 if(t>=80) t=0; far interrupt void TIM2_UP_IRQHandler (void) /中斷函數(shù) TIM2_SR1 = 0x00; t+;中斷向量:/*BASIC INTERRUPT VECTOR TABLE FOR STM8 devices *Copyright (c) 2007 STMicroelectronics */typedef void far (*interrupt_handler_t)(void);struct i

44、nterrupt_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;extern void _stext(); /* start

45、up routine */extern far interrupt void TIM2_UP_IRQHandler (void);struct interrupt_vector const _vectab = 0x82, (interrupt_handler_t)_stext, /* reset */0x82, NonHandledInterrupt, /* trap */0x82, NonHandledInterrupt, /* irq0 */0x82, NonHandledInterrupt, /* irq1 */0x82, NonHandledInterrupt, /* irq2 */0

46、x82, NonHandledInterrupt, /* 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, NonHand

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論