/* 添加包含芯片的头文件 */
#include<iostm8s103f3.h>
#include <stdint.h>
#include <stdio.h>
#ifdef UART_USE_STDIO
#if __ICCARM__
#include <yfuns.h>
#endif
#endif
#ifdef UART_USE_STDIO
#define UART_printf(fmt,args...) printf (fmt ,##args)
#endif
#ifdef UART_USE_STDIO
int printf(const char *fmt, ...);
#endif
unsigned int number1 ;
const char number2[] = "Test Printf OK!!";
/*******************************************************************************
**函数名称:void delay(unsigned int ms) Name: void delay(unsigned int ms)
**功能描述:大概延时
**入口参数:unsigned int ms 输入大概延时数值
**输出:无
*******************************************************************************/
void delay(unsigned int ms)
{
unsigned int x , y;
for(x = ms; x > 0; x--) /* 通过一定周期循环进行延时*/
for(y = 3000 ; y > 0 ; y--);
}
/*******************************************************************************
**函数名称:void UART1_Init(unsigned int baudrate)
**功能描述:初始化USART模块
**入口参数:unsigned int baudrate -> 设置串口波特率
**输出:无
*******************************************************************************/
void UART1_Init(unsigned int baudrate)
{
unsigned int baud; //
baud = 16000000 / baudrate; //设定串口相应波特率与串口时钟的分频数
//先写BRR2 , 再写BRR1
UART1_BRR2 = ((unsigned char)((baud & 0xf000) >> 8 )) | ((unsigned char)(baud & 0x000f)); //先写波特比率的高4位与低4位
UART1_BRR1 = ((unsigned char)((baud & 0x0ff0) >> 4)); //后写波特比率的中间8位
UART1_CR1_bit.UART0 = 0; //使能UART1
UART1_CR2_bit.RIEN = 1; //使能中断接收
UART1_CR2_bit.TEN = 1; //使能发送
UART1_CR2_bit.REN = 1; //接收使能
}
/*******************************************************************************
**函数名称:void Uart1_SendData(unsigned char data)
**功能描述:向串口发送寄存器写入一个字节数据
**入口参数:unsigned char data
**输出:无
*******************************************************************************/
void Uart1_SendData(unsigned char data)
{
while(!(UART1_SR&0X80)); //判断发送数据寄存器是否为空
UART1_DR = data; //向发送寄存器写入数据
}
int fputc(int ch, FILE *f)
{
Uart1_SendData(ch);
return ch;
}
#ifdef __CC_ARM // MDK Support
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef’ d in stdio.h. */
#elif __ICCARM__ /* IAR support */
size_t __write(int handle, const unsigned char * buffer, size_t size)
{
size_t nChars = 0;
if (buffer == 0)
{
/* This means that we should flush internal buffers. Since we*/
/* don't we just return. (Remember, "handle" == -1 means that all*/
/* handles should be flushed.)*/
return 0;
}
/* This function only writes to "standard out" and "standard err",*/
/* for all other file handles it returns failure.*/
if ((handle != _LLIO_STDOUT) && (handle != _LLIO_STDERR))
{
return _LLIO_ERROR;
}
/* Send data.*/
while (size--)
{
UART_WriteByte(UART_DebugInstance, *buffer++);
++nChars;
}
return nChars;
}
size_t __read(int handle, unsigned char * buffer, size_t size)
{
size_t nChars = 0;
uint16_t ch = 0;
if (buffer == 0)
{
/* This means that we should flush internal buffers. Since we*/
/* don't we just return. (Remember, "handle" == -1 means that all*/
/* handles should be flushed.)*/
return 0;
}
/* This function only writes to "standard out" and "standard err",*/
/* for all other file handles it returns failure.*/
if ((handle != _LLIO_STDIN) && (handle != _LLIO_STDERR))
{
return _LLIO_ERROR;
}
/* read data.*/
while (size--)
{
while(UART_ReadByte(UART_DebugInstance, &ch));
*buffer++ = (char)ch & 0xFF;
++nChars;
}
return nChars;
}
#endif /* comiler support */
/*******************************************************************************
**函数名称:static void UART_putstr(const char *str)
**功能描述:向USART1发送字符串
**入口参数:*str 发送字符串的首地址
**输出:发送完毕直接返回
*******************************************************************************/
static void UART_putstr(const char *str)
{
while(*str != '\0')
{
Uart1_SendData( *str++);
}
}
static void printn(unsigned int n, unsigned int b)
{
static char *ntab = "0123456789ABCDEF";
unsigned int a, m;
if (n / b)
{
a = n / b;
printn(a, b);
}
m = n % b;
Uart1_SendData( ntab[m]);
}
int UART_printf(const char *fmt, ...)
{
char c;
unsigned int *adx = (unsigned int*)(void*)&fmt + 1;
_loop:
while((c = *fmt++) != '%')
{
if (c == '\0') return 0;
Uart1_SendData(c);
}
c = *fmt++;
if (c == 'd' || c == 'l')
{
printn(*adx, 10);
}
if (c == 'o' || c == 'x')
{
printn(*adx, c=='o'? 8:16 );
}
if (c == 's')
{
UART_putstr((char*)*adx);
}
adx++;
goto _loop;
//return 0;
}
/* 主函数 */
int main(void)
{
number1 = 20;
asm("sim"); //关闭系统总中断
CLK_CKDIVR = 0x00; //CPUDIV = 1 HSIDIV = 1 内部时钟 = 16Mhz
UART1_Init(9600); //调用串口1初始化函数,并设置波特率为9600 bps
asm("rim"); //打开系统总中断
printf("\r\nTest!!");
while(1) //进入死循环,等待串口接收中断
{
delay(1000);
printf("\r\nNumber1 = %d , Number2 = %s" , number1 , number2);
}
}
/* 串口2接收数据中断服务函数 */
#pragma vector = 20 //设置串口1接收中断向量号 = 20
__interrupt void UART2_RX_RXNE(void)
{
unsigned char ch1;
UART1_SR_RXNE = 1; //清除中断标志
ch1 = UART1_DR; //读出串口接收到的数据
Uart1_SendData(ch1); //把接收到的数据再通过串口发送出去
}
没有合适的资源?快使用搜索试试~ 我知道了~
2.配套源码_寄存器版本.zip_488_STM8S103F3P6_UART SPI I2C_寄存器版本_所有程序
共87个文件
ewp:9个
xcl:8个
bat:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 77 浏览量
2022-07-14
04:56:24
上传
评论
收藏 1.05MB ZIP 举报
温馨提示
STM8S103F3P6寄存器版本所有程序 UART SPI I2C
资源推荐
资源详情
资源评论
收起资源包目录
2.配套源码_寄存器版本.zip (87个子文件)
科嵌单片机科技STM8S103F3开发板_实验13_USART中断接收_发送
Test13_USART_Receive_Send.ewp 57KB
Test13_USART_Receive_Send.dep 3KB
Test13_USART_Receive_Send.eww 179B
main.c 2KB
Test13_USART_Receive_Send.ewd 16KB
Backup (2) of Test13_USART_Receive_Send.ewp 42KB
Backup of Test13_USART_Receive_Send.ewp 41KB
settings
Test13_USART_Receive_Send.wsdt 26KB
Test13_USART_Receive_Send.dni 1KB
Test13_USART_Receive_Send.Debug.cspy.bat 2KB
Test13_USART_Receive_Send.Debug.general.xcl 440B
Test13_USART_Receive_Send.cspy.bat 1KB
Test13_USART_Receive_Send.Debug.driver.xcl 146B
Test13_USART_Receive_Send.dnx 3KB
Test13_USART_Receive_Send.dbgdt 7KB
Test13_USART_Receive_Send.Debug.cspy.ps1 2KB
Backup of Test13_USART_Receive_Send.ewd 11KB
Debug
List
main.lst 6KB
Test13_USART_Receive_Send.map 12KB
Exe
Test13_USART_Receive_Send.out 27KB
Test13_USART_Receive_Send.hex 2KB
Obj
main.pbi 192KB
Test13_USART_Receive_Send.pbd.browse 192KB
main.pbi.cout 10KB
Test13_USART_Receive_Send.pbd.linf 304B
Test13_USART_Receive_Send.pbd 192KB
main.o 13KB
Test13_USART_Receive_Send.ewt 157KB
科嵌单片机科技STM8S103F3开发板_实验14_USART_Printf发送
Backup of Test14_USART_Printf_Send.ewp 41KB
main.c 6KB
BuildLog.log 3B
Backup (2) of Test14_USART_Printf_Send.ewp 42KB
Test14_USART_Printf_Send.dep 4KB
Backup of Test14_USART_Printf_Send.ewd 11KB
Test14_USART_Printf_Send.eww 178B
Test14_USART_Printf_Send.ewt 157KB
settings
Test14_USART_Printf_Send.Debug.cspy.ps1 2KB
Test14_USART_Printf_Send.wsdt 28KB
Test14_USART_Printf_Send.Debug.general.xcl 433B
Test14_USART_Printf_Send.Debug.cspy.bat 2KB
Test14_USART_Printf_Send.dni 1KB
Test14_USART_Printf_Send.cspy.bat 1KB
Test14_USART_Printf_Send.Debug.driver.xcl 146B
Test14_USART_Printf_Send.dbgdt 56KB
Test14_USART_Printf_Send.dnx 3KB
Test14_USART_Printf_Send.ewp 57KB
Test14_USART_Printf_Send.ewd 16KB
Debug
List
main.lst 13KB
Test14_USART_Printf_Send.map 25KB
Exe
Test14_USART_Printf_Send.out 77KB
Test14_USART_Printf_Send.hex 20KB
Obj
Test14_USART_Printf_Send.pbd 267KB
Test14_USART_Printf_Send.pbd.linf 291B
main.pbi 269KB
Test14_USART_Printf_Send.pbd.browse 267KB
main.pbi.xcl 11KB
main.pbi.cout 10KB
main.o 29KB
科嵌单片机科技STM8S103F3开发板_实验15_USART_标准简易型Printf发送
Backup of Test15_TinyPrintf_Send.ewp 41KB
Test15_TinyPrintf_Send.eww 176B
Backup (2) of Test15_TinyPrintf_Send.ewp 41KB
main.c 5KB
BuildLog.log 3B
Test15_TinyPrintf_Send.ewd 16KB
Test15_TinyPrintf_Send.ewp 57KB
Backup of Test15_TinyPrintf_Send.ewd 11KB
settings
Test15_TinyPrintf_Send.cspy.bat 1KB
Test15_TinyPrintf_Send.dnx 3KB
Test15_TinyPrintf_Send.Debug.cspy.bat 2KB
Test15_TinyPrintf_Send.Debug.cspy.ps1 2KB
Test15_TinyPrintf_Send.wsdt 28KB
Test15_TinyPrintf_Send.dni 914B
Test15_TinyPrintf_Send.Debug.general.xcl 446B
Test15_TinyPrintf_Send.dbgdt 55KB
Test15_TinyPrintf_Send.Debug.driver.xcl 145B
Debug
List
Test15_TinyPrintf_Send.map 30KB
Exe
Test15_TinyPrintf_Send.out 97KB
Test15_TinyPrintf_Send.hex 24KB
Obj
main.pbi 313KB
main.pbi.xcl 11KB
main.pbi.cout 10KB
Test15_TinyPrintf_Send.pbd.browse 311KB
Test15_TinyPrintf_Send.pbd 311KB
Test15_TinyPrintf_Send.pbd.linf 319B
main.o 29KB
Test15_TinyPrintf_Send.dep 4KB
Test15_TinyPrintf_Send.ewt 157KB
共 87 条
- 1
资源评论
APei
- 粉丝: 63
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功