stm8C语言例程.doc_第1页
stm8C语言例程.doc_第2页
stm8C语言例程.doc_第3页
stm8C语言例程.doc_第4页
stm8C语言例程.doc_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

(1);名称 :流水灯;描述 : ;先从上至下点亮所有的LED,再逐个点亮单个LED;*/#include stm8s105s4.h#define uchar unsigned char#define uint unsigned intuchar table=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f; /存放点亮单个LED的代码void DelayMS(uint s) /延时子函数,约延时1ms uint i;for(s;s0;s-) for(i=0;i500;i+); void init(void) /stm8初始化函数PB_DDR = 0xff;PB_CR1 = 0xff; / 将PB设置成推挽输出PB_CR2 = 0x00;CLK_SWR = 0xE1; /选内部高速时钟作为主时钟CLK_CKDIVR = 0x08; /将CPU主频设置为2M(STM8默认的就是内部高速时钟的八分频,即2MH,这里只是告诉大家设置方法)void main()uchar i;init();while (1) /无限循环PB_ODR = 0xff; /先将所有的LED关闭for(i=0;i9;i+) /一开始是所有的LED熄灭,再逐点亮所有LED,共九种状态DelayMS(500); /延时500毫秒PB_ODR=1; /将PB_ODR向左移动一位,逐渐点亮所有LED for(i=0;i8;i+)PB_ODR=tablei; /将table中的数依次赋给PB_ODR ,从上至下依次点亮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 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(); /* 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, 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, 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, NonHandledInterrupt, /* irq23 */0x82, NonHandledInterrupt, /* irq24 */0x82, NonHandledInterrupt, /* irq25 */0x82, NonHandledInterrupt, /* irq26 */0x82, NonHandledInterrupt, /* irq27 */0x82, NonHandledInterrupt, /* irq28 */0x82, NonHandledInterrupt, /* irq29 */;(2):矩阵键盘、数码管:;描述 : 按下相应按键显示0到F中对应的数;*/#include #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;a0;a-) for(i=0;i255;i+);void init(void)PB_DDR=0XFF; PB_CR1=0XFF; / 将PB设置成推挽输出 PC_DDR=0X1F; PC_CR1=0XFF; / 将PC设置成推挽输出 PE_DDR=0x26; PE_CR1=0XFF; /将PE5,PE2,PE1为推挽输出,其他的设为上拉输入 PD_DDR=0X80; PD_CR1=0X80; /将PD7设为推挽输出/*这里的设置比LED的和数码管的设置要复杂,开始引入输入 模式,矩阵键盘列为输出,行为输入,请参考协会开发板原理图及STM8转51PDF资料*/*扫描方式也可为行输出,列输入,读者可以自己尝试着修改*/uchar keys(void) /此子函数看起来很长,实则只有前面一截内容,下面的“同理可得”uchar temp;PE_ODR=0X26; /将列中的PD7置为低电平,列中的其他三个置为高电平PD_ODR=0x00;delay(40); /这里要说明一下,由于矩阵键盘第一列和AD键盘共用一个IO口/AD键盘的RC会影响IO口电平变化的时间,这里需要延时一段时间,让电容C放电完毕if(PE_IDR&0x40)=0) /如果第一列的第四行按键被按下,则进入if语句内 delay(5); /延时一段时间,等待IO口电平稳定,即消抖if(PE_IDR&0x40)=0) /再次判断按键是否被按下,避免干扰 num=12; /如果第一列的第四行按键被按下,则令num=12,即数码管显示 为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;break; 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_IDR&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)!=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); return num; void main()uchar n;init();while(1) n=keys(); /把函数keys()的返回值num赋给nPB_ODR=tablen; PC_ODR|=0x00; /选择第一个数码管中断向量:/*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 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 _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, 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, NonHandledInterrupt, /* irq23 */0x82, NonHandledInterrupt, /* irq24 */0x82, NonHandledInterrupt, /* irq25 */0x82, NonHandledInterrupt, /* irq26 */0x82, NonHandledInterrupt, /* irq27 */0x82, NonHandledInterrupt, /* irq28 */0x82, NonHandledInterrupt, /* irq29 */;(3);名称 :定时器的使用 ;描述 : 数码管显示0F,利用定时器使得1秒变换一次;*/#include #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_CR2=0X00; PC_DDR=0XFF; PC_CR1=0XFF; PC_CR2=0X00; TIM2_EGR=0X01; /允许产生更新事件 TIM2_PSCR=0X01; /分频,使频率为1MHz TIM2_ARRH=0XC3; /更新后计数器的值 TIM2_ARRL=0X50; TIM2_CR1=0X05; /允许定时器工作 TIM2_IER=0X01; /允许更新中断 _asm(rim); /汇编语句,启动定时器 /注意!使用定时器时要定义中断函数入口,详请打开main.c下面的stm8_interrupt_vector.c,请按照注释进行修改 void main(void) uchar i=0,j; init(); while(1) PB_ODR=tablei; PC_ODR=0x02; /选择第二个数码管显示 if(t=20) /这里设置50ms进入一次中断,t=20刚好为1秒 t=0; /t清零 i+; if(i=16) i=0; /由于数组中只有16个数,所以i最大只能为15,否则显示会出现乱码现象 far interrupt void TIM2_UP_IRQHandler (void) /中断函数 TIM2_SR1 = 0x00; /进入中断时TIM2_SR1最低位会被硬件自动置一,进入中断后必须将其清零,否则无法再次产生中断 t+; /进入中断后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 order 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_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, 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 */ /使用定时器要把这里的“NonHandledInterrupt”改为“TIM2_UP_IRQHandler”,即中断函数名 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, NonHandledInterrupt, /* irq23 */ 0x82, NonHandledInterrupt, /* irq24 */ 0x82, NonHandledInterrupt, /* irq25 */ 0x82, NonHandledInterrupt, /* irq26 */ 0x82, NonHandledInterrupt, /* irq27 */ 0x82, NonHandledInterrupt, /* irq28 */ 0x82, NonHandledInterrupt, /* irq29 */;(3) (4):蜂鸣器:定时器方式;名称 :蜂鸣器 ;描述 : 利用定时器产生;*/#include #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=0X01; /允许产生更新事件TIM2_PSCR=0X01; /分频,使频率为1MHz TIM2_ARRH=0XC3; /更新后计数器的值TIM2_ARRL=0X50; /定时50ms,用于控制发声时间 TIM2_CR1=0X05; /允许定时器工作TIM2_IER=0X01; /允许更新中断TIM3_EGR=0X01; /允许产生更新事件TIM3_PSCR=0X01; /分频,使频率为1MHz TIM3_ARRH=0X00; /更新后计数器的值TIM3_ARRL=0Xfa; /这里定时0.25ms,用于产生所需的频率 TIM3_CR1=0X05; /允许定时器工作TIM3_IER=0X01; /允许更新中断_asm(rim); /汇编语句,启动定时器void delay(uchar s)uchar i;for(s;s0;s-) for(i=0;i=20&t240) /第二秒产生1KHz频率声音 if(t3%4=40&t260) /第三秒产生500Hz频率声音 if(t3%8=60) t2=0; if(t3=8) t3=0;far interrupt void TIM2_UP_IRQHandler (void) /定时器2中断函数 TIM2_SR1 = 0x00; /清零TIM2_SR1 t2+;far interrupt void TIM3_UP_IRQHandler (void) /定时器三中断函数 TIM3_SR1 = 0x00; /清零TIM2_SR1 t3+;中断向量:/*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 order 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);extern far interrupt void TIM3_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 */0x82, 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, NonHandledInterrupt, /* irq12 */0x82, TIM2_UP_IRQHandler, /* irq13 */0x82, 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 */0x82, NonHandledInterrupt, /* irq22 */0x82, NonHandledInterrupt, /* irq23 */0x82, NonHandledInterrupt, /* irq24 */0x82, NonHandledInterrupt, /* irq25 */0x82, NonHandledInterrupt, /* irq26 */0x82, NonHandledInterrupt, /* irq27 */0x82, NonHandledInterrupt, /* irq28 */0x82, NonHandledInterrupt, /* irq29 */;(5) 蜂鸣器:stm8BEEP方式;名称 :STM8自带蜂鸣器使用;描述 : ;注!此功能需用ST Link辅助设置,协会实验板无法正常工作,这里仅作参考;*/#include #define uchar unsigned charuchar t;void init(void) /初始化函数 CLK_ICKR|=0x08; / 打开芯片内部的低速振荡器LSI while(CLK_ICKR&0x10)=0); / 等待振荡器稳定 TIM2_EGR=0X01; /允许产生更新事件 TIM2_PSCR=0X01; /分频,使频率为1MHz TIM2_ARRH=0XC3; /更新后计数器的值 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)&(t=60)&(t=80) t=0; far interrupt void TIM2_UP_IRQHandler (void) /中断函数 TIM2_SR1 = 0x00; t+;中断向量:/*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 order 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_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, NonHandledInterrupt, /* irq6 */0x82, NonHandledInterrupt, /* irq7 */0x82, NonHandledInterrupt, /* irq8 */0x82, NonHandledInterrupt, /* irq9 */0x82, NonHandledInterrupt, /* irq10 */0x82, NonHandledInterrupt, /* irq11 */0x82, NonHandledInterrupt, /*

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论