IAR下STM8S207单片机各模块的无库例程

来源:本站
导读:目前正在解读《IAR下STM8S207单片机各模块的无库例程》的相关信息,《IAR下STM8S207单片机各模块的无库例程》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《IAR下STM8S207单片机各模块的无库例程》的详细说明。
简介:TIM/ADC/CLOCK/EEPROM/SPI/UART/WWDG/IWDG等不使用库的应用例程,包含SPI双机通讯、定时器的捕获、PWM各功能的实现等;原理图有2个错误:1,晶振电容为22P;2,按键电容位置错;

以下模块全部已调试通过:

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

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

@Fn: ConfigADC10()

@Br: 配置ADC10

@Pa: 无

@Rt: 无

@Sp: 无

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

void ConfigADC10( void ){

ADC_CSR = ( 1 << EOCIE )

+ ( 1 << CH02 ) + ( 1 << CH01 ) + ( 1 << CH00 );

/* 中断使能, 选中CH7 */

ADC_CR2 = ( 1 << ALIGN ); /* 右对齐 */

ADC_CR1 = ( 1 << SPSEL02 ) + ( 1 << SPSEL01 ) + ( 1 << SPSEL00 )

+ ( 1 << CONT ) + ( 1 << ADON );

/* Fadc = Fmaster / 18, 持续转换, ADON写1唤醒ADC */

ADC_CR1 |= ( 1 << ADON ); /* 当ADON为1时再写1则启动转换 */

}

suzhwt 发表于 2010-9-21 15:43

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

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

@Fn: ConfigClock()

@Br: 配置处理器的时钟

@Pa: 无

@Rt: 无

@Sp: 无

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

void ConfigClock( void ){

CLK_ICKR = ( 1 << HSIEN ); /* 使能内部高速RC OSC */

CLK_ECKR = ( 1 << HSEEN ); /* 打开外部晶体振荡器 */

while(( CLK_ECKR & 0x02 ) == 0 ); /* ( 1 << HSERDY ) */

CLK_SWR = F_MASTER_HSE; /* 指定切换的HSE主时钟 */

while(( CLK_SWCR & 0x08 ) == 0 ); /* ( 1 << SWIF ) */

CLK_SWCR |= ( 1 << SWEN ); /* 执行切换 */

CLK_CKDIVR = 0x00; /* Fcpu = Fmaster = 18MHz */

CLK_PCKENR1 = ( 1 << TM1_CLK_ENABLE )

+ ( 1 << TM3_CLK_ENABLE )

+ ( 1 << TM2_CLK_ENABLE )

+ ( 1 << TM4_CLK_ENABLE )

+ ( 1 << UART1_CLK_ENABLE )

+ ( 1 << SPI_CLK_ENABLE );

CLK_PCKENR2 = ( 1 << ADC_CLK_ENABLE );

}

suzhwt 发表于 2010-9-21 15:43

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

#define FIRST_SECURITY_KEY 0xAE

#define SECOND_SECURITY_KEY 0x56

#define ADD_EEPROM_S8 0x4000

/* STM8S207S8的CODE空间为64K,而EEPROM的大小为1536字节,即:3页 * 512节/页 */

/* EEPROM的KEY写入与FLASH的KEY正好相反 */

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

@Fn: InitialFlashReg()

@Br: 初始化闪存寄存器组

@Pa: 无

@Rt: 无

@Sp: 无

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

void InitialFlashReg( void ){

FLASH_CR1 = FLASH_CR1_RESET_VALUE;

FLASH_CR2 = FLASH_CR2_RESET_VALUE;

FLASH_NCR2 = FLASH_NCR2_RESET_VALUE;

FLASH_IAPSR &= ( uchar )(~( 1 << DUL )); /* 清除只读DATA区解锁 */

FLASH_IAPSR &= ( uchar )(~( 1 << PUL )); /* 清除程序区解锁 */

}

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

@Fn: UnlockFlash()

@Br: 解锁存储器

@Pa: 存储器类型: 不是程序区就是只读数据区

@Rt: 无

@Sp: 2个密钥的操作序列正好相反

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

void UnlockFlash( uchar Type ){

if( Type == UNLOCK_FLASH_TYPE ) /* 解锁程序区 */

{

FLASH_DUKR = SECOND_SECURITY_KEY;

FLASH_DUKR = FIRST_SECURITY_KEY;

}

else /* 解锁只读数据区 */

{

FLASH_DUKR = FIRST_SECURITY_KEY;

FLASH_DUKR = SECOND_SECURITY_KEY;

}

}

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

@Fn: LockFlash()

@Br: 锁定存储器

@Pa: 存储器类型:不是程序区就是只读数据区

@Rt: 无

@Sp: 无

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

void LockFlash( uchar Type ){

if( Type == UNLOCK_FLASH_TYPE )

{

FLASH_IAPSR &= ~( 1 << PUL );

}

else

{

FLASH_IAPSR &= ~( 1 << DUL );

}

}

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

@Fn: InitialFlashReg()

@Br: 写入一字节到指定只读数据区[EEPROM]

@Pa: 指定EEPROM地址与数据内容

@Rt: 无

@Sp: 无

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

void WriteByteToFLASH( ulong Address, uchar Data ){

*(( __far uchar * ) Address ) = Data;

}

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

@Fn: Write4BytToFlash()

@Br: 写入4字节到指定EEPROM的连续4字节空间

@Pa: EEPROM地址与LONG型数据内容

@Rt: 无

@Sp: 数据写入方式为小端模式

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

void Write4BytToFlash( ulong Address, ulong Data ){

/* Enable Word Write Once */

FLASH_CR2 |= ( 1 << WPRG );

FLASH_NCR2 &= ( uchar )( ~( 1 << NWPRG ));

*((( __far uchar * )Address ) + 3 ) = *(( uchar * )( &Data )); /* Write one byte - from Highest address*/

*((( __far uchar * )Address ) + 2 ) = *(( uchar * )( &Data ) + 1 ); /* Write one byte*/

*((( __far uchar * )Address ) + 1 ) = *(( uchar * )( &Data ) + 2 ); /* Write one byte*/

*(( __far uchar * )Address ) = *(( uchar * )( &Data ) + 3 ); /* Write one byte - from higher address*/

}

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

@Fn: ReadByteEEPROM()

@Br: 从EEPROM中读取1字节

@Pa: 指定EEPROM地址

@Rt: 读取的CHAR型内容

@Sp: 无

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

uchar ReadByteEEPROM( ulong Address ){

return(*(( __far uchar* ) Address )); /* Read byte */

}

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

@Fn: Read4BytFromFlash()

@Br: 从EEPROM中读取连续的4字节

@Pa: 指定EEPROM地址

@Rt: 读取的LONG内容

@Sp: 小端模式

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

ulong Read4BytFromFlash( ulong Address ){

ulong i;

ulong Temp[2];

Temp[0] = *(( __far uchar * )Address );

Temp[0] |= ( *((( __far uchar * )Address ) + 1 ) << 8 );

Temp[1] = *((( __far uchar * )Address ) + 2 );

Temp[1] |= ( *((( __far uchar * )Address ) + 3 ) << 8 );

i = Temp[0] + ( Temp[1] << 16 );

free( Temp );

return i;

}

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

@Fn: EraseByteFLASH()

@Br: 擦除EEPROM中内容

@Pa: 指定EEPROM地址

@Rt: 无

@Sp: 无

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

void EraseByteFLASH( uint Address ){

*(( __near uchar * ) Address ) = 0x00;

}

void Test( void ){

ulong i;

UnlockFlash( UNLOCK_EEPROM_TYPE );

Write4BytToFlash( ADD_EEPROM_S8, 0x12345678 );

LockFlash( UNLOCK_EEPROM_TYPE );

i = Read4BytFromFlash( ADD_EEPROM_S8 );

i = i;

}

suzhwt 发表于 2010-9-21 15:43

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

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

@Fn: ConfigPortE()

@Br: 配置端口E

@Pa: 无

@Rt: 无

@Sp: 无

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

void ConfigPortE( void ){

PE_DDR = ( 1 << PE6 ) + ( 1 << PE5 );

PE_CR1 = 0;

PE_CR2 = 0;

}

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

@Fn: ConfigGPIO()

@Br: 配置端口

@Pa: 无

@Rt: 无

@Sp: 无

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

void ConfigGPIO( void ){

PG_DDR = 0x03; /* 输出 */

PG_CR1 = 0x03; /* 推挽模式 */

PG_CR2 = 0x00; /* 速度2MHz */

ConfigPortE();

}

suzhwt 发表于 2010-9-21 15:44

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

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

@Fn: ConfigIWDG()

@Br: IWDG初始化

@Pa: 无

@Rt: 无

@Sp: 需要选择字支持

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

void ConfigIWDG( void ){

IWDG_KR = IWDG_KEY_ACCESS; /* 写入修改值使能键值 */

IWDG_PR = ( 1 << IWDG_PR02 ) + ( 1 << IWDG_PR01 ); /* 1.02s时长 */

IWDG_RLR = 0xFF;

IWDG_KR = IWDG_KEY_REFRESH; /* 写入后恢复到保护状态 */

IWDG_KR = IWDG_KEY_EBABLE; /* 启动看门狗 */

}

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

@Fn: ClearIWDG()

@Br: 清看门狗IWDG

@Pa: 无

@Rt: 无

@Sp: 无

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

void ClearIWDG( void ){

IWDG_KR = IWDG_KEY_REFRESH; /* 清看门狗 */

}

suzhwt 发表于 2010-9-21 15:44

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

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

@Fn: ConfigInterrupt()

@Br: 配置中断输入口

@Pa: 无

@Rt: 无

@Sp: 无

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

void ConfigInterrupt( void ){

PA_DDR &= ~( 1 << PA6 ); /* 配置为输入口 */

PA_CR1 = ( 1 << C16 ); /* 配置上拉电阻 */

PA_CR2 = ( 1 << C26 ); /* 使能中断 */

CPU_CCR |= ( 1 << I1 ) + ( 1 << I0 ); /* 改为最高优先级 */

EXTI_CR1 = 0; /* 下降沿和低电平触发 */

}

suzhwt 发表于 2010-9-21 15:44

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

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

@Fn: ConfigSPI()

@Br: 配置SPI接口

@Pa: 工作模式

@Rt: 无

@Sp: 无

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

void ConfigSPI( uchar WorkMode ){

SPI_CR1 = 0x00;

SPI_CR2 = ( uchar )( 1 << SSM ); /* SSI位的值代替NSS脚控制从设备的选择 */

SPI_ICR = 0x00;

SPI_SR = ( uchar )( 1 << SPI_TXE );

SPI_CR1 = ( uchar )(( uchar )( 1 << LSBFIRST )

+ ( uchar )( 1 << BR02 ) /* 波特率 = 2 ^ ( BR[2:0] + 1 ) = Fmaster/32 */

+ ( uchar )( 1 << SPI_CPOL )

+ ( uchar )( 1 << SPI_CPHA ));

if( WorkMode == SPI_MASTER_MODE )

{

SPI_CR2 |= ( uchar )( 1 << SSI );

SPI_CR1 |= ( uchar )( 1 << MSTR );

}

else

{

SPI_CR2 &= ~( uchar )( 1 << SSI );

SPI_CR1 &= ~( uchar )( 1 << MSTR );

}

SPI_ICR |= ( uchar )( 1 << RXIE ); /* 使能SPI接收中断 */

}

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

@Fn: EnablesSPI()

@Br: SPI状态

@Pa: 不是打开就是关闭

@Rt: 无

@Sp: 无

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

void EnablesSPI( uchar Type ){

if( Type == SPI_ENABLES )

{

SPI_CR1 |= ( uchar )( 1 << SPE );

}

else

{

SPI_CR1 &= ~( uchar )( 1 << SPE );

}

}

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

@Fn: TransmitBySPI()

@Br: 从SPI传输数据

@Pa: 数据组冲以及发送帧的长度

@Rt: 无

@Sp: 无

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

void TransmitBySPI( uchar *Buff, uchar Len ){

for( uchar i = 0; i < Len; i++ )

{

SPI_DR = *Buff++;

while( SPI_SR & ( 1 << TXE ) == 0 );

}

}

suzhwt 发表于 2010-9-21 15:44

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

/* Excel2000 expression as: v = sin( DEGREE * PI()/180 ) */

/* 0 degree = 0x024A / 2 = 0x0125 = 293 */

/* 10 degree = 0x0125 + sin10 = 344 */

#define SIN_WAVE_LEN 36

const uint SIN_TABLE[SIN_WAVE_LEN] = {

9376, 9312, 9088, 8752, 8272, 7696,

7040, 6288, 5504, 4688, 3872, 3088,

2352, 1680, 1104, 624, 288, 64,

0, 64, 288, 624, 1104, 1680,

2352, 3088, 3872, 4688, 5504, 6288,

7040, 7696, 8272, 8752, 9088, 9312

};

volatile uchar ucSinIndex = 0;

#pragma vector = TM3CC_VECT

__interrupt void TIM3_UPDATE_ISR( void ){

if( TIM3_SR1 & ( 1 << CC1IF ))

{

TIM3_SR1 &= ~( 1 << CC1IF );

LoadValueOfPWM();

/* 固定脉宽试验 */

// TIM3_CCR1H = ( uchar )( PERCENT_080 >> 8 );

// TIM3_CCR1L = ( uchar )( PERCENT_080 );

/* F = 30Hz, Duty = 80% High-level */

}

}

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

@Fn: ConfigT3_PWM2()

@Br: Timer3的初始化为PWM模式1

@Pa: 无

@Rt: 无

@Sp:

Timer2,Timer3,Timer5 是通用16位定时器.

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

void ConfigT3_PWM2( void ){

TIM3_PSCR = ( 1 << PSC02 );

/* Configure TIM3 prescaler = 4 */

/* Fck_cnt = Fck_psc / 2 ^[3:0] */

TIM3_CCMR1 = ( 1 << OC1M02 ) + ( 1 << OC1M01 ) + ( 1 << OC1M00 )

+ ( 1 << OC1PE );

/* 输出比较1预装载使能, PWM模式2 */

TIM3_CCER1 = ( 1 << CC1P ) + ( 1 << CC1E );

/* OC1低电平有效, 使能TIM3_CH1引脚功能 */

TIM3_ARRH = 0x2F; /* 确定频率 */

TIM3_ARRL = 0xFF;

TIM3_CNTRH = 0x24;

TIM3_CNTRL = 0xA0;

/* ARR一定要大于CNT,否则输出波形不正确 */

TIM3_CCR1H = 0; /* 初始化TIM3_CH1引脚缓冲器为零 */

TIM3_CCR1L = 0;

TIM3_SR1 &= ~( 1 << CC1IF ); /* 清空中断标志 */

TIM3_CR1 = ( 1 << ARPE ) + ( 1 << CEN ); /* 自动重载及计数器使能 */

TIM3_IER = ( 1 << CC1IE ); /* 使能中断 */

}

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

@Fn: LoadValueOfPWM()

@Br: 载入波表值

@Pa: 无

@Rt: 无

@Sp:

Timer2,Timer3,Timer5 是通用16位定时器.

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

void LoadValueOfPWM( void ){

TIM3_CCR1H = ( uchar )( SIN_TABLE[ ucSinIndex ] >> 8 ); /* 2.5赫正弦波 */

TIM3_CCR1L = ( uchar )( SIN_TABLE[ ucSinIndex ] );

ucSinIndex++;

if( ucSinIndex >= SIN_WAVE_LEN )

{

ucSinIndex = 0;

}

}

suzhwt 发表于 2010-9-21 15:45

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

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

@Fn: ConfigTimer1()

@Br: 捕获TIM1CH1[PC1]引脚上按键产生的脉冲宽度.

@Pa: 无

@Rt: 无

@Sp: 无

* 时钟在CLK_PCKENR1中被关闭时所有设置不能写入到寄存器中.

* Timer1 是高级16位定时器.

* 向下计数能得到精确的时间长度值.

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

void ConfigTimer1( void ){

PC_DDR &= ~( 1 << PC1 ); /* 配置为输入口 */

PC_CR1 = ( 1 << C11 ); /* 配置上拉电阻 */

TIM1_PSCRH = ( 1 << PSC11 ) + ( 1 << PSC10 );

TIM1_PSCRL = ( 1 << PSC07 ) + ( 1 << PSC06 )

+ ( 1 << PSC05 ) + ( 1 << PSC02 )

+ ( 1 << PSC01 ) + ( 1 << PSC00 );

/* 分频率为PSCR[15:0] + 1 = 1000 */

TIM1_CNTRH = 0x46;

TIM1_CNTRL = 0x50;

TIM1_ARRH = 0x46;

TIM1_ARRL = 0x50;

/*

18,000,000

CNTR = ------------ = 0x4650

1,000

*/

TIM1_CR2 = 0; /* CC1输入管脚连到TIM1_CH1 */

TIM1_CCMR1 = ( 1 << CC1S00 ); /* 连接到TIM1_CH1引脚,无滤波,无分频 */

/* 注:CC1S仅在通道关闭时(TIM1_CCER1寄存器的CC1E=0)才是可写的. */

TIM1_CCER1 = ( 1 << CC1P ) /* 下降沿有效 */

+ ( 1 << CC1E ); /* 捕获使能 */

TIM1_SR1 &= ~( 1 << CC1IF ); /* 清除捕获标志 */

TIM1_SR2 &= ~( 1 << CC1OF ); /* 清除重复捕获标志 */

TIM1_IER = ( 1 << CC1IE ); /* 捕获中断使能 */

TIM1_CR1 = ( 1 << DIR ) /* 向下计数 */

+ ( 1 << CEN ); /* 使能计数 */

}

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

@Fn: CalculatePulseLen()

@Br: 计算脉冲的时间长度

@Pa: 无

@Rt: 无

@Sp: 无

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

void CalculatePulseLen( void ){

ulong Temp;

uchar Buff[7];

if( uiTenor & TM1_CAP_FLAG )

{

if( uiCaptureT1[0] <= uiCaptureT1[1] )

{

uiCaptureT1[0] += PRELOAD_VALUE_1S;

}

Temp = uiCaptureT1[0] - uiCaptureT1[1]; /* 捕获计数长度 */

/*

计算脉宽

脉冲时钟 clk = 捕获值 * 分频倍率

= i * 1000

脉冲时钟 11,995,575

t = ---------- = ------------ = 0.66642 s

系统时钟 18,000,000

*/

Temp = Temp * 1000;

Temp = Temp / 18; /* 放大1,000,000倍 */

Buff[6] = Temp % 10;

Temp /= 10;

Buff[5] = Temp % 10;

Temp /= 10;

Buff[4] = Temp % 10;

Temp /= 10;

Buff[3] = Temp % 10;

Temp /= 10;

Buff[2] = Temp % 10;

Temp /= 10;

Buff[1] = Temp % 10;

Temp /= 10;

Buff[0] = Temp % 10;

memcpy( &ucCapBuff[0], &Buff[0], SEVEN_LEN );

uiTenor &= ~TM1_CAP_FLAG;

}

free( Buff );

}

suzhwt 发表于 2010-9-21 15:45

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

/* Timer4, Timer6 */

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

@Fn: ConfigTimer4()

@Br: Timer4初始化为定时器方式

@Pa: 无

@Rt: 无

@Sp: 计算CNT值的方法与其他处理器的不一样.

Timer4, Timer6 是基本8位定时器.

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

void ConfigTimer4( void ){

TIM4_PSCR = ( 1 << PSC02 ) + ( 1 << PSC01 ) + ( 1 << PSC00 );

/* Configure TIM4 prescaler = 128 */

/* Fck_cnt = Fck_psc / 2 ^[2:0] */

TIM4_ARR = 0xFF; /* TIM4的自动装载寄存器 */

TIM4_CNTR = 0xFF; /* TIM4的计数器 */

/*

18,000,000

CNTR = ------------ = 140,625

128

中断一次的时间

t = 256 / 140,625 = 0.0018204 s

*/

TIM4_CR1 = ( 1 << ARPE ) + ( 1 << CEN ); /* 允许自动重装载,使能计数器 */

TIM4_IER = ( 1 << UIE ); /* 允许更新中断 */

}

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

/* Timer2, Timer3, Timer5 */

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

@Fn: ConfigTimer2()

@Br: Timer2初始化为定时器方式

@Pa: 无

@Rt: 无

@Sp: 计算CNT值的方法与其他处理器的不一样.

Timer2,Timer3,Timer5 是通用16位定时器.

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

void ConfigTimer2( void ){

TIM2_PSCR = ( 1 << PSC03 ) + ( 1 << PSC01 );

/* Configure TIM2 prescaler = 1024 */

/* Fck_cnt = Fck_psc / 2 ^[3:0] */

TIM2_ARRH = 0x44; /* TIM2的自动装载寄存器 */

TIM2_ARRL = 0xAA;

TIM2_CNTRH = 0x44; /* TIM2的计数器 */

TIM2_CNTRL = 0xAA;

/*

18,000,000

CNTR = ------------ = 0x44AA

1024

*/

TIM2_CR1 = ( 1 << ARPE ) + ( 1 << CEN ); /* 允许自动重装载,使能计数器 */

TIM2_IER = ( 1 << UIE ); /* 允许更新中断 */

}

suzhwt 发表于 2010-9-21 15:45

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

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

@Fn: ConfigUART1()

@Br: 配置USART1

@Pa: 无

@Rt: 无

@Sp: 无

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

void ConfigUART1( void ){

UART1_SR = ( 1 << RXNE ); /* 使能接收 */

UART1_CR1 = 0; /* 1个起始位,8个数据位, */

UART1_CR3 = 0; /* 1个停止位 */

UART1_CR4 = 0;

UART1_CR5 = 0;

/*

Fmaster 18,000,000

Baud rate = ------------ = ---------- = 7500 = 0x1D4C

UART_DIV 2400

UART_BRR1 = UART_DIV[11:4]

UART_BRR2 = UART_DIV[15:12] | UART_DIV[3:0]

*/

UART1_BRR2 = 0x1C;

UART1_BRR1 = 0xD4; /* 2400bps @ 18MHz,先写BRR2再写BRR1 */

UART1_CR2 = ( 1 << RIEN ) + ( 1 << TEN ) + ( 1 << REN );

/* 接收中断使能, 发送与接收使能 */

}

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

@Fn: TransmitFrame()

@Br: 从UART1发送数据

@Pa: 缓冲指针及长度

@Rt: 无

@Sp: 无

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

void TransmitFrame( uchar *Buff, uchar Len ){

for( uchar i = 0; i < Len; i++ )

{

while(( UART1_SR & ( 1 << TXE )) == 0 );

UART1_DR = *Buff++;

_NOP();

}

}

suzhwt 发表于 2010-9-21 15:46

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

/*

对窗口看门狗的应用理解:

在一个程序线程中,如果在特定的时间内,操作没有被完

成,则由该窗口看门狗实现复位,而不是传统操作上的死机,

例如,

在由CPU对一个芯片进行读数据的操作过程中,因硬件的

问题导致CPU在此操作中死循环而无法退出,那么,可以由这个

WWDG设置一个时间级限,如果在此极限中若没有将WWDG停止,

则复位发生.

*/

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

@Fn: ConfigWWDG()

@Br: WWDG初始化

@Pa: 无

@Rt: 无

@Sp: 无

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

void ConfigWWDG( void ){

WWDG_WR = 0x7F;

WWDG_CR = ( uchar )(( 1 << WDGA ) + 0x7F );

WWDG_WR = 0x6F;

}

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

@Fn: RefreshWWDG()

@Br: 更新WWDG

@Pa: 无

@Rt: 无

@Sp: 无

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

void RefreshWWDG( void ){

uchar Temp;

Temp = WWDG_CR & ~( 1 << WDGA );

if( Temp < WWDG_WR )

{

WWDG_CR = ( uchar )(( 1 << WDGA ) | 0x7F );

}

}

suzhwt 发表于 2010-9-21 15:46

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

#pragma vector = EXTI_A_VECT

__interrupt void PA_ISR( void ){

if(( PA_IDR & ( 1 << IDR6 )) == 0 )

{

uiTenor |= PA6_INT_FLAG;

}

}

#pragma vector = TM1CC_VECT

__interrupt void TIM1_CAPTURE_ISR( void ){

if( TIM1_SR1 & ( 1 << CC1IF ))

{

if( TIM1_CCER1 & ( 1 << CC1P )) /* 下降沿产生的中断 */

{

uiCaptureT1[0] = ( TIM1_CCR1H << 8 );

uiCaptureT1[0] |= TIM1_CCR1L;

TIM1_CCER1 &= ~( 1 << CC1P ); /* 改为上升沿捕获 */

}

else /* 上升沿产生的中断 */

{

uiCaptureT1[1] = ( TIM1_CCR1H << 8 );

uiCaptureT1[1] |= TIM1_CCR1L;

TIM1_CCER1 |= ( 1 << CC1P ); /* 改为下降沿捕获 */

uiTenor |= TM1_CAP_FLAG; /* 一段低电平时间捕获 */

}

}

}

#pragma vector = TM2OV_VECT

__interrupt void TIM2_UPDATE_ISR( void ){

if( TIM2_SR1 & ( 1 << UIF ))

{

TIM2_SR1 &= ~( 0x01 );

uiTenor |= TIME2_FLAG;

}

}

#pragma vector = TM4OV_VECT

__interrupt void TIM4_UPDATE_ISR( void ){

if( TIM4_SR & ( 1 << UIF ))

{

TIM4_SR &= ~( 0x01 );

uiTenor |= TIME4_FLAG;

}

}

#pragma vector = UART1RX_VECT

__interrupt void UART1RX_ISR( void ){

ucBuff[ ucIndex++ ] = UART1_DR;

if( ucIndex >= 2 )

{

if( ucIndex >= ucBuff[1] )

{

ucIndex = 0;

uiTenor |= UART1_RX_FLAG;

}

}

}

#pragma vector = ADC_VECT

__interrupt void ADC10_ISR( void ){

ADC_CSR &= ~( 1 << EOC );

uiADC = ( ADC_DRH << 8 );

uiADC |= ADC_DRL;

}

#pragma vector = SPI_VECT

__interrupt void SPI_ISR( void ){

if( SPI_SR & ( 1 << SPI_RXNE ))

{

SPI_DR = SPI_DR;

}

}

suzhwt 发表于 2010-9-21 15:46

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Globals.h"

/* Author: EighthArmy @ July.2010 */

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

@Fn: ConfigCPU()

@Br: 配置处理器及外围

@Pa: 无

@Rt: 无

@Sp: 无

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

void ConfigCPU( void ){

_DI();

ConfigClock();

ConfigGPIO();

ConfigInterrupt();

ConfigTimer1();

ConfigTimer2();

ConfigT3_PWM2();

ConfigTimer4();

ConfigADC10();

ConfigUART1();

InitialFlashReg();

//ConfigWWDG();

_EI();

}

suzhwt 发表于 2010-9-21 15:47

#include "ioSTM8S207S8.h"

#include "Includes.h"

#include "Constants.h"

#include "Functions.h"

#include "Variables.h"

/* Author: EighthArmy @ July.2010 */

void main( void ){

uint i = 0;

ConfigCPU();

while( 1 )

{

if( uiTenor & TIME2_FLAG )

{

PG_ODR ^= 0x01;

PE_ODR ^= ( 1 << PE5 );

uiTenor &= ~TIME2_FLAG;

}

if( uiTenor & PA6_INT_FLAG )

{

PG_ODR ^= 0x02;

ucBuff[0] = 0x11;

ucBuff[1] = 0x22;

ucBuff[2] = 0x33;

ucBuff[3] = 0x44;

ucBuff[4] = 0x55;

ucBuff[5] = 0x66;

ucBuff[6] = 0x77;

ucBuff[7] = 0x88;

TransmitFrame( &ucBuff[0], 8 );

uiTenor &= ~PA6_INT_FLAG;

}

if( uiTenor & TIME4_FLAG )

{

i++;

if( i >= 100 )

{

PE_ODR ^= ( 1 << PE6 );

i = 0;

}

uiTenor &= ~TIME4_FLAG;

}

Comm1Events();

CalculatePulseLen();

//RefreshWWDG(); /* 清窗口看门狗,窗口看门狗在硬件复位 */

}

}

压缩包中的SPI模块包含主从通讯,可以实现主从SPI方式通讯。

相比之下,速度要比UART爽多了。

学STM8的朋友千万不要错过啊!!

这都是心血和经验啊!

提醒:《IAR下STM8S207单片机各模块的无库例程》最后刷新时间 2024-03-14 00:53:40,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《IAR下STM8S207单片机各模块的无库例程》该内容的真实性请自行鉴别。