1. STM8S系列程序示例
/* MAIN.C file
* Function:实现按键S1控制LED和蜂鸣器
* Copyright (c) 2002-2005 STMicroelectronics
*/
#include "stm8s105c4.h"
_Bool LED1 @PA_ODR:6; //定义PA.6输出寄存器为LED1
_Bool Beep @PD_ODR:4; //定义PD.4输出寄存器为蜂鸣器
_Bool S1 @PD_IDR:2; //定义PD.2输入寄存器为按键
//I/O初始化
void GPIO_Init(void)
{
PA_DDR = 0x40; //PA.6推挽输出
PA_CR1 = 0x40;
PD_DDR = 0x10;//PD.2上拉输入 、PD.4推挽输出
PD_CR1 = 0x14;
}
main()
{
GPIO_Init();
while(1)
{
if(S1==0) //如果有按键则灯亮,蜂鸣器响
{
LED1 = 1;
Beep = 1;
}
else
{
Beep = 0;
LED1 = 0;
}
}
}
2. 谁用IAR给 STM8S003F3 单片机用C语言写个程序 用一个按键控制一个LED
这个很简单吧 做一个变量然后取反不就行了嘛
这是最简单的基础知识了。
3. 本人正在学习stm8,使用IAR编程,求代码把蜂鸣器弄响
你用的是“ ST MCU 三合一体验套件 ” 里面的那套STM8S的开发板嚒·?
如果是的话可以参考以下例程:是在微雪电子那采购的官方套件光盘中的示例程序,不知道能不能帮到你! 有分的话给点分用用,嘿嘿!
/*============================================================================*/
/* PROJECT: DRIVING BUZZER THROUGH STM8 TIMER2 PWM DEMO SYSTEM */
/* MODULE: main.c */
/* COMPILER: STM8 Cosmic C Compiler */
/* DATE: Feb 2009 */
/*----------------------------------------------------------------------------*/
/* DESCRIPTION: Demonstration firmware for STM8 Mini Kit */
/* playing a tune through its buzzer. */
/*============================================================================*/
/******************************************************************************
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* COPYRIGHT 2008 STMicroelectronics
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "STM8S105C_S.h" /* Registers and memory mapping file. */
/******************************************************************************/
/* Macro definitions
/******************************************************************************/
/* Music instruction and note coding. */
#define _END_ 0xFF /* Music END. */
#define _PAUSE_ 0xFE /* Pause between different tunes. */
/* Note tone definition...... */
#define _FA0 0x00 /* FA- */
#define _SOL0 0x01 /* SOL- */
#define _LA0 0x02 /* LA- */
#define _SI0 0x03 /* SI- */
#define _DO 0x04 /* DO */
#define _RE 0x05 /* RE */
#define _MI 0x06 /* MI */
#define _FA 0x07 /* FA */
#define _SOL 0x08 /* SOL */
#define _LA 0x09 /* LA */
#define _SI 0x0A /* SI */
#define _DO2 0x0B /* DO+ */
#define _M 0x0C /* MUTE */
#define _RE2 0x0D /* RE+ */
#define _SOL2 0x0E /* SOL+ */
#define _FAd 0x0F /* FA# */
/* Note length definition...... */
#define sq 0x10 /* Semiquaver notes. */
#define q 0x20 /* Quaver notes. */
#define qp 0x30 /* 1.5 quaver notes. */
#define c 0x40 /* Crotchet notes. */
#define cn 0x60 /* 1.5 crotchet notes.*/
#define m 0x80 /* Minim notes. */
/*a note is defined here by a combination of a tone and a length, both parts
being easily retrievable with the AND binary operator: note = tone+length,
and tone&length=0*/
/******************************************************************************/
/* RAM SEGMENT variables */
/******************************************************************************/
/* Global variable used to store the ADC result. */
unsigned int AD_Value;
/* Global variable used to store the Key pressed for changing octave. */
unsigned char Flag_Octave_Chg;
/* Global variable used as index for the array of notes: position in the tune.*/
unsigned int current_note = 0;
/* Global variable used as index for the array of notes. */
unsigned char c1,c1_buf;
/* Music note coding ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* FA0 SOL0 LA0 SI0 DO RE MI FA SOL LA SI DO2 MUTE RE2 SOL2 FA# */
/* TIM2 CCR1 High byte. */
const unsigned char Low_Note_h[] /* Lower octave */
={0x2C,0x27,0x23,0x1F,0x1D,0x1A,0x17,0x16,0x13,0x11,0x0F,0x0E,0x00, 0x0D, 0x09, 0x15 };
const unsigned char Hi_Note_h[] /* Higher octave */
={0x16,0x13,0x11,0x0F,0x0E,0x0D,0x0B,0x0B,0x09,0x08,0x07,0x07,0x00, 0x06, 0x04, 0x0A };
/* TIM2 CCR1 Low byte. */
const unsigned char Low_Note_l[] /* Lower octave */
={0xA4,0xDC,0x82,0xA1,0xEE,0xA9,0xAC,0x62,0xEE,0xC1,0xD0,0xF0,0x00, 0x4F, 0xF7, 0x2C };
const unsigned char Hi_Note_l[] /* Higher octave */
={0x52,0xEE,0xC1,0xD0,0xF7,0x54,0xD6,0x31,0xF7,0xE0,0xE8,0x78,0x00, 0xA7, 0xFB, 0x96 };
/* The actual tune sequence: an array of notes. */
const unsigned char tune[] =
{
_M+sq, _M+sq, /* Two "buffer" mutes needed to manage smoothly the */
/* current_note/current_note_init comparison. */
/*------------------ DO RE MI FA SOL LA SI DO2 --------------------------*/
/*------------------ DO2 SI LA SOL FA MI RE DO --------------------------*/
_DO+c,_RE+c,_MI+c,_FA+c,_SOL+c,_LA+c,_SI+c,_DO2+c,_M+m, _PAUSE_,
_DO2+c,_SI+c,_LA+c,_SOL+c,_FA+c,_MI+c,_RE+c,_DO+c,_M+m, _PAUSE_,
/*---------------------------- 新年好 ------------------------------------*/
_DO+c,_DO+c,_DO+c,_M+sq,_DO+m,_SOL0+m,_MI+c,_MI+c,_M+sq,_MI+m,_DO+m,_DO+c,
_MI+c,_M+sq,_SOL+m,_SOL+m,_FA+c,_MI+c,_M+sq,_RE+m,_RE+m,_M+sq,_RE+c,_MI+c,
_FA+m,_M+sq,_FA+m,_MI+c,_RE+c,_MI+m,_DO+m,_M+sq,_DO+c,_MI+c,_RE+m,_SOL0+m,
_SI0+c,_RE+c,_DO+m,_DO+m,_PAUSE_,
/*--------------------------- 两只老虎 -----------------------------------*/
_DO+c,_RE+c,_MI+c,_DO+c,_DO+c,_RE+c,_MI+c,_DO+c,_MI+c,_FA+c,_SOL+m,_MI+c,
_FA+c,_SOL+m,_SOL+qp,_LA+sq,_SOL+qp,_FA+sq,_MI+c,_DO+c,_SOL+qp,_LA+sq,
_SOL+qp,_FA+sq,_MI+c,_DO+c,_M+sq,_DO+c,_SOL0+c,_DO+m,_M+sq,_DO+c,_SOL0+c,
_DO+m,_M+sq,_PAUSE_,
/*---------------------------- 甜蜜蜜 ------------------------------------*/
_MI+m,_SOL+c,_LA+c,_MI+m,_MI+c,_DO+c,_RE+cn,_DO+q,_RE+c,_MI+q,_SOL+q,_MI+m,
_MI+m,_RE+c,_RE+c,_RE+c,_RE+q,_MI+q,_RE+q,_DO+cn,_LA0+q,_SOL0+cn,_DO+m,
_DO+c,_RE+c,_MI+cn,_RE+q,_MI+q,_RE+q,_MI+q,_SOL+q,_RE+m,_RE+m,_RE+m,_RE+m,
_MI+m,_SOL+c,_LA+c,_MI+m,_MI+c,_DO+c,_RE+cn,_DO+q,_RE+c,_MI+q,_SOL+q,_MI+m,
_MI+m,_RE+c,_RE+c,_RE+c,_RE+q,_MI+q,_RE+q,_DO+cn,_LA0+q,_SOL0+cn,_DO+m,
_DO+c,_MI+c,_RE+q,_DO+q,_DO+c,_LA0+q,_SOL0+cn,_DO+m,_DO+m,_DO+m,_DO+m,_MI+m,
_MI+m,_SOL0+m,_LA0+q,_DO+q,_SOL0+q,_LA0+q,_DO+m,_DO+m,_DO+m,_DO+m,_LA0+c,
_SI0+c,_LA0+c,_SI0+c,_LA0+c,_LA0+q,_DO+q,_LA0+q,_SOL0+q,_SOL0+c,_MI+m,_MI+m,
_MI+m,_MI+m,_LA0+c,_SI0+c,_LA0+c,_SI0+c,_LA0+c,_LA0+q,_DO+q,_LA0+q,_SOL0+q,
_SOL0+c,_MI+m,_MI+m,_MI+m,_M+c,_SOL+q,_LA+q,_SOL+m,_M+c,_SOL+q,_LA+q,_SOL+m,
_M+c,_SOL+q,_LA+q,_SOL+c,_SOL+q,_LA+q,_SOL+c,_SOL+q,_LA+q,_SOL+m,_SOL+m,
_MI+m,_SOL+c,_LA+c,_MI+m,_MI+c,_DO+c,_RE+cn,_DO+q,_RE+c,_RE+q,_SOL+q,_MI+m,
_MI+m,_RE+c,_RE+c,_RE+c,_RE+q,_MI+q,_RE+q,_DO+cn,_LA0+q,
_SOL0+cn,_DO+m,_DO+c,_MI+c,_RE+q,_DO+q,_DO+c,_LA0+q,_SOL0+cn,_DO+m,_DO+m,
_DO+m,_DO+m,_MI+m,_MI+m,_SOL0+m,_LA0+q,_DO+q,_SOL0+q,_LA0+q,_DO+m,_DO+m,
_DO+m,_DO+m,_PAUSE_,
/*------------------ _END_ marks to end of the tune ----------------------*/
_END_
};
/******************************************************************************/
/* Function definitions */
/******************************************************************************/
/* -------------------------------------------------------------------------- */
/* ROUTINE NAME: Buzz_Init */
/* INPUT/OUTPUT: None. */
/* DESCRIPTION: Initialize the TIM2 as PWM mode for BUZZ control. */
/* -------------------------------------------------------------------------- */
void Buzz_Init ( void )
{
PD_DDR |= 0x10; /* Configure PD4 as output (for the PWM). */
PD_CR1 |= 0x10; /* PD4 Push pull output. */
TIM2_CCMR1 |= 0x70; /* Output mode PWM2. */
TIM2_CCER1 |= 0x03; /* CC polarity low,enable PWM output */
TIM2_ARR = 0; /* Freq control register: ARR */
TIM2_CCR1 = 0; /* Dutycycle control register: CCR */
TIM2_PSCR |= 0x00; /* fCK_CNT is equal to fCK_PSC. */
TIM2_CR1 |= 0x01; /* Enable TIM2. */
current_note = 1;
}
/* -------------------------------------------------------------------------- */
/* ROUTINE NAME: Buzz_Wait */
/* INPUT/OUTPUT: Note ration (4bit MSB information) / None. */
/* DESCRIPTION: 1) Sample AIN voltage and store in AD_Value. */
/* 2) Polling wait routine for note ration(based on 4ms delay).*/
/* ration: Quaver (2), crotchet (4) or minim (8) selection. */
/* ---------------------------------------------------------------------------*/
void Buzz_Wait(unsigned char ration)
{
int i = 0;
unsigned char uc = 0;
unsigned long Temp;
/* Sample AIN voltage in ADC single mode. */
ADC_CR1 |= 0x01; /* First set ADON to power on the ADC mole. */
i = 6; /* 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). */
/* Store ADC value to AD_Value */
AD_Value = ((((unsigned int)ADC_DRH)<<2)+ADC_DRL)>>2;
if (AD_Value < 0x01)
{ AD_Value = 0x01; }
if (AD_Value > 0xC0)
{ AD_Value = 0xC0; }
if (Flag_Octave_Chg==1)
{
Temp = ((((unsigned int) Low_Note_h [c1_buf])<<8)+Low_Note_l [c1_buf]);
}
else
{
Temp = ((((unsigned int) Hi_Note_h [c1_buf])<<8)+Hi_Note_l [c1_buf]);
}
Temp = (Temp*AD_Value)>>9;
/* The new ty cycle value is written in CCR */
TIM2_CCR1H=(unsigned char)(((unsigned int)Temp & 0xff00)>>8);
TIM2_CCR1L=(unsigned char)((unsigned int)Temp & 0x00ff);
/* Delay time = ration * Y */
while ( uc < ration ) /* The following loop is run "ration" times. */
{
while ( i < 1200 ) /* This loop "Y" waits approximately 4.3ms. */
{
i++;
}
i = 0;
uc++;
}
}
/* -------------------------------------------------------------------------- */
/* ROUTINE NAME: Buzz_PlayTune */
/* INPUT/OUTPUT: None. */
/* DESCRIPTION: Plays a music score (one tune at a time). */
/* -------------------------------------------------------------------------- */
void Buzz_PlayTune ( void )
{
unsigned ui;
unsigned long temp_DCR;
unsigned char temp;
while(1)
{
if(tune[current_note] == _END_)
{
/* End of the music, reset to beginning. 1 is a mute at the */
/* beginning of the array of notes; differs from 0. */
current_note = 1;
break;
}
else if(tune[current_note] == _PAUSE_)
{
/* End of a tune, save the position in the music, stop playing. */
current_note++;
break;
}
else
{
c1 = tune[current_note];
/* Loads a note (or mute) on the relevant registers */
/* The note information is carried only by the 4 lowest bits. */
c1_buf= c1 & 0x0f;
if (Flag_Octave_Chg==1)
{
temp_DCR = ((((unsigned int)Low_Note_h [c1_buf])<<8)+Low_Note_l[c1_buf]);
TIM2_ARRH = Low_Note_h [c1_buf];
TIM2_ARRL = Low_Note_l [c1_buf];
}
else
{
temp_DCR = ((((unsigned int) Hi_Note_h [c1_buf])<<8)+Hi_Note_l [c1_buf]);
TIM2_ARRH = Hi_Note_h [c1_buf];
TIM2_ARRL = Hi_Note_l [c1_buf];
}
temp_DCR = (temp_DCR*AD_Value)>>9;
/* The new ty cycle value is written in DCR0. */
temp=((unsigned int)temp_DCR & 0xff00)>>8;
TIM2_CCR1H=(unsigned char)temp;
temp=((unsigned int)temp_DCR & 0x00ff);
TIM2_CCR1L=(unsigned char)temp;
/* Waits for the ration of the note. */
/* The ration info is carried by the 4 highest bits. */
Buzz_Wait(c1&0xF0);
/* Progressing in the array of notes: the tune. */
current_note++;
}
}
}
/* -------------------------------------------------------------------------- */
/* ROUTINE NAME: GPIO_Init */
/* INPUT/OUTPUT: None. */
/* DESCRIPTION: Initialize the GPIO for LED,TLI. */
/* -------------------------------------------------------------------------- */
void GPIO_Init(void)
{
/* LED IO Configuration */
/* LD3: PD3 */
/* LD2: PD1 */
/* LD1: PD0 */
PD_DDR |= 0x0D; /* Output. */
PD_CR1 |= 0x0D; /* PushPull. */
PD_CR2 = 0x00; /* Output speed up to 2MHz. */
/* PD7 external interrupt */
EXTI_CR1 = 0x00; /* External interrupt (TLI) sensitivity. */
EXTI_CR2 = 0x00;
PD_DDR &=~0x80; /* PD7: Input */
PD_CR2 |= 0x80; /* Enable TLI interrupt. */
}
/* -------------------------------------------------------------------------- */
/* ROUTINE NAME: CLK_Init */
/* INPUT/OUTPUT: None. */
/* DESCRIPTION: Initialize the clock source */
/* -------------------------------------------------------------------------- */
void CLK_Init(void)
{
/* Configure HSI prescaler*/
CLK_CKDIVR &= ~0x10; /* 01: fHSI= fHSI RC output/2. */
/* Configure CPU clock prescaler */
CLK_CKDIVR |= 0x01; /* 001: fCPU=fMASTER/2. */
}
/* -------------------------------------------------------------------------- */
/* ROUTINE NAME: ADC_Init */
/* INPUT/OUTPUT: None. */
/* DESCRIPTION: Initialize the AD converter. */
/* -------------------------------------------------------------------------- */
void ADC_Init(void)
{
ADC_CR2 = 0x00;
ADC_CR1 = 0x00;
ADC_CSR = 0x03;
ADC_TDRL = 0x20;
}
/* -------------------------------------------------------------------------- */
/* ROUTINE NAME: TIM_Init */
/* INPUT/OUTPUT: None. */
/* DESCRIPTION: Initialize the TIM4 as LED timebase. */
/* -------------------------------------------------------------------------- */
void TIM_Init(void)
{
/* TIM4 Peripheral Configuration */
/* Time Base configuration */
TIM4_PSCR = 0x04; /* Configure TIM4 prescaler. */
TIM4_ARR = 0xFF; /* Configure TIM4 period. */
/*TIM4 counter enable */
TIM4_CR1 |= 0x01; /* Enable TIM4. */
TIM4_IER |= 0x01; /* Enable TIM4 OVR interrupt. */
}
/* -------------------------------------------------------------------------- */
/* ROUTINE: main */
/* main entry */
/* -------------------------------------------------------------------------- */
void main ( void )
{
unsigned int j;
_asm("sim"); /* Disable interrupts. */
Flag_Octave_Chg=0;
CLK_Init();
GPIO_Init();
TIM_Init();
ADC_Init();
_asm("rim"); /* Enable interrupts. */
Buzz_Init (); /* Init TIMER peripheral. */
while ( 1 )
{
/* Play the current score. */
Buzz_PlayTune();
Buzz_Wait(0x80); /* Wait around one second. */
}
}
/* --------------------------- End of file -----------------------------------*/
4. STM8的C语言延时及外部中断编程
研究了下,你描述的逻辑好像有矛盾,
我疑问如下:
控制过程如下:
pin17(PC7)收到低电平脉冲信号(脉冲长度多少?),
计时器启动(计数时间5秒?),
pin19(PD2)输出高电平(在计数计数结束后输出低电平?);
当m为奇数时,pin13(PC3)输出高电平(PIN14输出低电平?);
当m为偶数时,pin14(PC4)输出高电平(PIN13输出低电平?)。
最终计数结束后输出状态如何?
pin10(PA3)有高电平脉冲信号输入时(脉冲长度多少?),
pin19(PD2)停止输出(输出低电平还是高电平?);
如果在一个计时周期内(5秒吗?),
pin10(PA3)没有高电平信号输入(一直维持低电平吗?),
则pin18(PD1)输出高电平(有高电平脉冲信号时输出低电平?)。
pin15(PC5)有高电平脉冲信号输入时(脉冲长度多少?),
pin13(PC3)停止输出(输出低电平还是高电平?);
如果在一个计时周期内(5秒吗?),
pin15(PC5)没有高电平信号输入(一直维持低电平吗?),
且计数器值m为奇数,则pin18(PD1)输出高电平(m是偶数时怎么处理?)。
。。。。。。。。。。。
主要程序到底是触发后输出一个序列电平还是测量输入脉冲?
5. IAR环境 STM8S空指令C语言怎么写
#define nop() {_asm("nop\n");} /* No Operation */
6. 用STM8S105单片机写一个输出频率为2KHz的方波,C语言 没用过stm8单片机,求入门资料。谢谢!
//此例程是通过TIM2 CH1(PD4脚)通道来输出一个频率2K 占空比可调的方波,占空比可通过PD7脚的按键调
#include "STM8S105K.h"
typedef unsigned char u8;
typedef unsigned int u16;
u16 value;
void SystemInit(void)
{
CLK_CKDIVR = 0x08; // 16M内部RC经2分频后系统时钟为8M
CLK_PCKENR1 |= 0x60; //使能TIM2与TIM3与主频连接
PD_CR2 |= 0x80; //使能PD7口外部中断
}
void GPIO_init(void)
{
PD_DDR = 0x1F; //配置PD端口的方向寄存器全输出
PD_CR1 = 0x1F; //设置PD为推挽输出
}
void TIM2_init(void) //TIM2 CH1 工作于模式1
{
TIM2_CCMR1= 0x60; // PWM 模式 1,TIM2 CH1
TIM2_CCER1= 0x03; // CC1配置为输出
TIM2_ARRH = 0x07; // 配置PWM分辨率为10位,ARR=0x07D0
TIM2_ARRL = 0xD0; // PWM频率=8M/0x07D0=2000Hz
TIM2_CR1 |= 0x01; // 计数器使能,开始计数
}
void init_devices(void)
{
asm("sim");
SystemInit();
GPIO_init();
TIM2_init();
_asm("rim"); //开总中断
}
void main( void )
{
init_devices();
while(1)
TIM2_CCR1=value;//改变value值可改变频率
}
/****************************************************************************
*** 函数名 : @near @interrupt void TLI_IRQHandler (void)
*** 功能描述: 中断服务程序
按下PD7口按键来改变占空比
*****************************************************************************/
@near @interrupt void TLI_IRQHandler (void)
{
PD_CR2 &= 0x7F; //关PD7外部中断
value+=50;
while(value>1000)
value=0;
PD_CR2 |= 0x80; //使能PD7口外部中断
return;
}
7. stm8的空指令延时c语言怎样写
单片机的C语言关于延时函数主要有两种一种是用for循环,通过单片机执行空指令达到延时的目的如:for(i=0;i<100;i++){;}这个简单的语句会执行100次空指令每一次指令的时间可以大概确定因此这个是最简单的延时函数第二种是通过定时器的方式来实现定时器是通过对单片机的晶振进行计数然后在定时器中断服务函数里面实现定时时间的计算及设置51单片机的定时器0中断服务函数为voidtime0()interrupt1{}
8. STM8S系列程序示例
/*********************************************
* STM8S105* TIM2 TIM3输出PWM PD0 PD3 TIM3输出PWM,*********************************************/#include "STM8S105K.h"
#include "STM8S105_CLOCK.h"
typedef unsigned char u8;
typedef unsigned int u16;u16 value;
/*******************************************************************************
*** 函数名 : void SystemInit(void)
*** 功能描述: 系统初始化
*** 函数说明: 系统全局初始化
******************************************************************************/
void SystemInit(void)
{ SystemClock_Init();
CLK_PCKENR1 |= 0x60; //使能TIM2与TIM3与主频连接
PD_CR2 |= 0x80; //使能PD7口外部中断
}
/*******************************************************************************
*** 函数名 : void main(void)
*** 功能描述: 主函数
*** 函数说明:
******************************************************************************/
void GPIO_init(void)
{
PD_DDR = 0x1F; //配置PD端口的方向寄存器全输出
PD_CR1 = 0x1F; //设置PD为推挽输出
PB_DDR|= 0x04; //PB2输出
PB_CR1|= 0x04; //PB2推挽输出
PB_ODR|= 0x04; //开5V电源
}
void TIM2_init(void) //TIM2 CH1 工作于模式1
{
// TIM2_CCMR2= 0x60; // PWM 模式 1
TIM2_CCMR1= 0x60; // PWM 模式 1,TIM2 CH1
TIM2_CCER1= 0x03; // CC1配置为输出
//TIM2_CCER2= 0x03; // CC3使能
TIM2_ARRH = 0x03; // 配置PWM分辨率为10位,ARR=0x3FF
TIM2_ARRL = 0xFF; // PWM频率=8M/0x03FF=7820Hz
TIM2_CR1 |= 0x01; // 计数器使能,开始计数
}
void TIM3_init(void) //TIM3 CH1 ch2工作于模式2,1
{
TIM3_CCMR1= 0x70; //PWM模式2
TIM3_CCMR2= 0x60; // PWM 模式 1
TIM3_CCER1= 0x33; // CC1 CC2配置为输出,CH1 CH2
TIM3_ARRH = 0x03; // 配置PWM分辨率为10位,ARR=0x3FF
TIM3_ARRL = 0xFF; // PWM频率=8M/0x03FF=7820Hz
TIM3_CR1 = 0x01; // 计数器使能,开始计数
} void Run(void)
{ //TIM2_CCR2H = (unsigned char)(value>>8); // 更新CC2比较寄存器
//TIM2_CCR2L = (unsigned char)(value);
TIM2_CCR1=value;
//TIM2_CCR3=value;
TIM3_CCR2=value;
TIM3_CCR1=value; //注意每个通道都要设CCR
} void init_devices(void)
{
_asm("sim");
SystemInit();
GPIO_init();
TIM2_init();
TIM3_init();
_asm("rim");
} void main( void )
{
init_devices();
while(1)
Run();
} /****************************************************************************
*** 函数名 : @near @interrupt void TLI_IRQHandler (void)
*** 功能描述: 中断服务程序
*** 函数说明:
*****************************************************************************/
@near @interrupt void TLI_IRQHandler (void) {
PD_CR2 &= 0x7F; //关PD7外部中断
value+=50;
while(value>1000)
value=0; PD_CR2 |= 0x80; //使能PD7口外部中断
return;
}
9. STM8S的c语言指令中,循环左移的命令是什么
用汇编指令 RLC(左移位)
asm("RLC NumSwitch");