完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include “stm8l15x.h”
#include “delay.h” #include “stdarg.h” #include “stdio.h” //定义LED与按键接口 #define LED_GPIO_PORT GPIOC #define LED_GPIO_PINS GPIO_Pin_4 #define KEY_GPIO_PORT GPIOD #define KEY_GPIO_PINS GPIO_Pin_4 uint8_t HexTable[]={‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘A’,‘B’,‘C’,‘D’,‘E’,‘F’}; void USART1_SendStr(unsigned char *Str) { while(*Str!=0)//不为结束 { USART_SendData8(USART1,*Str); //发送数据 while(!USART_GetFlagStatus (USART1,USART_FLAG_TXE));//等待发送完毕 Str++;//下一个数据 } } void USART1_SendHex(unsigned char dat) { USART_SendData8(USART1,‘0’); while(!USART_GetFlagStatus (USART1,USART_FLAG_TXE));//等待发送完毕 USART_SendData8(USART1,‘x’); while(!USART_GetFlagStatus (USART1,USART_FLAG_TXE));//等待发送完毕 USART_SendData8(USART1,HexTable[dat>>4]); while(!USART_GetFlagStatus (USART1,USART_FLAG_TXE));//等待发送完毕 USART_SendData8(USART1,HexTable[dat&0x0f]); while(!USART_GetFlagStatus (USART1,USART_FLAG_TXE));//等待发送完毕 USART_SendData8(USART1,’ '); while(!USART_GetFlagStatus (USART1,USART_FLAG_TXE));//等待发送完毕 } /发送串口数据/ void send_uart_data(uint8_t data) { while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET); USART_SendData8(USART1,data); } /* 功能:将int型数据转为2,8,10,16进制字符串 参数:value — 输入的int整型数 str — 存储转换的字符串 radix — 进制类型选择 注意:8位单片机int字节只占2个字节 */ char *sky_itoa(int value, char *str, unsigned int radix) { char list[] = “0123456789ABCDEF”; unsigned int tmp_value; int i = 0, j, k = 0; if (NULL == str) { return NULL; } if (2 != radix && 8 != radix && 10 != radix && 16 != radix) { return NULL; } if (radix == 10 && value < 0) { //十进制且为负数 tmp_value = (unsigned int)(0 - value); str[i++] = ‘-’; k = 1; } else { tmp_value = (unsigned int)value; } //数据转换为字符串,逆序存储 do { str[i ++] = list[tmp_value%radix]; tmp_value /= radix; } while(tmp_value); str = ‘ ’; //将逆序字符串转换为正序 char tmp; for (j = k; j < (i+k)/2; j++) { tmp = str[j]; str[j] = str[i-j-1+k]; str[i-j-1+k] = tmp; } return str; } /* 功能:将double型数据转为字符串 参数:value — 输入的double浮点数 str — 存储转换的字符串 eps — 保留小数位选择,至少保留一个小数位,至多保留4个小数位 注意:8位单片机int字节只占2个字节 */ void sky_ftoa(double value, char *str, unsigned int eps) { unsigned int integer; double decimal; char list[] = “0123456789”; int i = 0, j, k = 0; //将整数及小数部分提取出来 if (value < 0) { decimal = (double)(((int)value) - value); integer = (unsigned int)(0 - value); str[i ++] = ‘-’; k = 1; } else { integer = (unsigned int)(value); decimal = (double)(value - integer); } //整数部分数据转换为字符串,逆序存储 do { str[i ++] = list[integer%10]; integer /= 10; } while(integer); str = ‘ ’; //将逆序字符串转换为正序 char tmp; for (j = k; j < (i+k)/2; j++) { tmp = str[j]; str[j] = str[i-j-1+k]; str[i-j-1+k] = tmp; } //处理小数部分 if (eps < 1 || eps > 4) { eps = 4; } //精度问题,防止输入1.2输出1.19等情况 double pp = 0.1; for (j = 0; j <= eps; j++) { pp *= 0.1; } decimal += pp; while (eps) { decimal *= 10; eps --; } int tmp_decimal = (int)decimal; str[i ++] = ‘.’; k = i; //整数部分数据转换为字符串,逆序存储 do { str[i ++] = list[tmp_decimal%10]; tmp_decimal /= 10; } while(tmp_decimal); str = ‘ ’; //将逆序字符串转换为正序 for (j = k; j < (i+k)/2; j++) { tmp = str[j]; str[j] = str[i-j-1+k]; str[i-j-1+k] = tmp; } str = ‘ ’; } void mprintf(char * Data, …) { const char *s; int d; char buf[16]; uint8_t txdata; va_list ap; va_start(ap, Data); while ( * Data != 0 ) { if ( * Data == 0x5c ) { switch ( *++Data ) { case ‘r’: txdata = 0x0d; send_uart_data(txdata); Data ++; break; case ‘n’: txdata = 0x0a; send_uart_data(txdata); Data ++; break; default: Data ++; break; } } else if ( * Data == ‘%’) { switch ( *++Data ) { case ‘s’: s = va_arg(ap, const char *); for ( ; s; s++) { send_uart_data(((uint8_t *)s)); } Data++; break; case ‘d’: d = va_arg(ap, int); sky_itoa(d, buf, 10); for (s = buf; s; s++) { send_uart_data(((uint8_t *)s)); } Data++; break; case ‘x’: { d = va_arg(ap, int); sky_itoa(d, buf, 16); for (s = buf; s; s++) { send_uart_data(((uint8_t *)s)); } Data++; break; } case ‘f’: { double num = va_arg(ap, double); sky_ftoa(num, buf, 4); for (s = buf; s; s++) { send_uart_data(((uint8_t )s)); } Data++; break; } default: Data++; break; } } else { send_uart_data(((uint8_t *)Data)); Data++; } } } int main( void ) { u16 u16_adc1_value; CLK_PeripheralClockConfig (CLK_Peripheral_USART1,ENABLE);//开启ADC/USART时钟 CLK_PeripheralClockConfig (CLK_Peripheral_ADC1,ENABLE);//开启ADC/USART时钟 USART_Init(USART1,115200,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,USART_Mode_Tx);//USART初始化,9600,8N1 USART_Cmd (USART1,ENABLE);//使能USART ADC_Init (ADC1,ADC_ConversionMode_Single,ADC_Resolution_12Bit,ADC_Prescaler_1);//单次转换,12位,ADC转换时钟1分频 ADC_Cmd(ADC1,ENABLE);//使能ADC1 ADC_ChannelCmd (ADC1,ADC_Channel_16,ENABLE);//使能通道11------PB2 while(1) { //USART1_SendStr("ADC转换结果为: "); ADC_SoftwareStartConv (ADC1);//开始ADC软件转换 while(!ADC_GetFlagStatus (ADC1,ADC_FLAG_EOC));//等待转换结束 ADC_ClearFlag (ADC1,ADC_FLAG_EOC);//清除对应标志 u16_adc1_value=ADC_GetConversionValue (ADC1);//获取转换值 // USART1_SendHex((u16_adc1_value>>8)); //USART1_SendHex((u16_adc1_value&0xff)); //USART_SendData9(USART1, u16_adc1_value); mprintf("%drn",u16_adc1_value ); /*mprintf("STM8L start...rn"); mprintf("%f %f %frn", 1.2, 12.36, -1.2364568); mprintf("%x %x %xrn", 0x1035, 0x0830, 0x2018); mprintf("%d %d %drn", 12, -12345, 2); mprintf("STM8L end...rn");*/ delay_10us(100); } } |
|
|
|
只有小组成员才能发言,加入小组>>
3280 浏览 9 评论
2958 浏览 16 评论
3460 浏览 1 评论
9004 浏览 16 评论
4052 浏览 18 评论
1115浏览 3评论
573浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
571浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2303浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1860浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 18:03 , Processed in 1.266651 second(s), Total 79, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号