生活随笔
收集整理的这篇文章主要介绍了
Basic脚本解释器移植到STM32
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
上次讲了LUA移植到STM32,这次讲讲Basic脚本解释器移植到STM32。在STM32上跑Basic脚本,同样可以跟穿戴设备结合,也可以作为初学者学习MCU的入门工具,当然前提是有人做好Basic的STM32交互实现。这里使用的是uBasic开源脚本解释器(http://dunkels.com/adam/ubasic/),不过uBasic不支持完整的Basic算法,所以用起来略费心,如果有好的Basic开源脚本解释器,ANSI-C实现的,欢迎推荐。。。
本文实现的功能是输入以下basic脚本:
[vb] view plaincopyprint?
10 v=1 20 for p = 4 to 7 40 write "gpioa",p,v 50 next p 60 if v=0 then goto 10 70 if v=1 then v=0 80 goto 20 run
实现的功能是同时把4个LED灯同时开后再同时关,通过自定义的命令 write来实现,p是IO脚,v是IO的数值。
[vb] view plaincopyprint?
write "gpioa",p,v
如下图:
本文代码可以到这里下载http://download.csdn.net/detail/hellogv/7391265。
main.c的源码如下,通过USART1来发送Basic脚本到STM32,另外还要通过readline()来做些预处理,例如收到“run”这个字符串就表示脚本结束开始运行:
[cpp] view plaincopyprint?
#include "stm32f10x_lib.h" #include <assert.h> #include "stdio.h" #include <stdlib.h> #include <string.h> #include "ubasic.h" void RCC_Configuration(void) { ErrorStatus HSEStartUpStatus; RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON ); HSEStartUpStatus = RCC_WaitForHSEStartUp(); if (HSEStartUpStatus == SUCCESS) { RCC_HCLKConfig(RCC_SYSCLK_Div1 ); RCC_PCLK2Config(RCC_HCLK_Div1 ); RCC_PCLK1Config(RCC_HCLK_Div2 ); FLASH_SetLatency(FLASH_Latency_2 ); FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable ); RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 ); RCC_PLLCmd(ENABLE); while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY ) == RESET) ; RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK ); while (RCC_GetSYSCLKSource() != 0x08) ; } RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); } void USART_Configuration(void) { USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART1, &USART_ClockInitStructure); USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No ; USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); } int fputc(int ch, FILE *f) { USART_SendData(USART1, (u8) ch); while (USART_GetFlagStatus(USART1, USART_FLAG_TC ) == RESET); return ch; } int getKey(void) { while (!(USART1 ->SR & USART_FLAG_RXNE )); return ((int) (USART1 ->DR & 0x1FF)); } bool readLines(char *s) { bool isString = FALSE; char ch; char *p = s; if(*p!='\0') return FALSE; while (1) { ch = getKey(); if (ch == '\"') { isString = !isString; } if (ch == '\r') continue; else { if (isString) *p++ = ch; else *p++ = tolower(ch); } if (*(p-3) == 'r' &&*(p-2)=='u' &&*(p-1)=='n'){ *(p-3) = '\0'; break; } } return TRUE; } #define _MAX_LINE_LENGTH 256 int main(void) { RCC_Configuration(); GPIO_Configuration(); USART_Configuration(); char line[_MAX_LINE_LENGTH]; while(1){ memset(line, 0, _MAX_LINE_LENGTH); if(readLines(line)){ ubasic_init(line); do { ubasic_run(); } while(!ubasic_finished()); } }; return 0; }
实现write命令的源码如下:
[cpp] view plaincopyprint?
#include "tokenizer.h" #include "stm32f10x_lib.h" #include <string.h> #include <ctype.h> #include <stdlib.h> GPIO_TypeDef *gpio_x; u16 gpio_pin_x; u16 gpio_value; struct GPIO_KEYWORD gpio_kt; struct PIN_KEYWORD pin_kt; #define GET_ARRAY_LEN(array,len){len = (sizeof(array) / sizeof(array[0]));} struct GPIO_KEYWORD { char *keyword; GPIO_TypeDef *token; }; static const struct GPIO_KEYWORD gpio_keywords[] = { { "GPIOA", GPIOA }, { "GPIOB", GPIOB }, { "GPIOC", GPIOC }, { "GPIOD", GPIOD }}; struct PIN_KEYWORD { u16 keyword; u16 token;; }; static const struct PIN_KEYWORD pin_keywords[17] = { { 0, GPIO_Pin_0 }, { 1, GPIO_Pin_1 }, { 2, GPIO_Pin_2 }, { 3, GPIO_Pin_3 }, { 4, GPIO_Pin_4 }, { 5, GPIO_Pin_5 }, { 6, GPIO_Pin_6 }, { 7, GPIO_Pin_7 }, { 8, GPIO_Pin_8 }, { 9, GPIO_Pin_9 }, { 10, GPIO_Pin_10 }, { 11, GPIO_Pin_11 }, { 12, GPIO_Pin_12 }, { 13, GPIO_Pin_13 }, { 14, GPIO_Pin_14 }, { 15, GPIO_Pin_15 }}; void init_my_statement(){ gpio_x=NULL; gpio_pin_x=NULL; gpio_value=NULL; } void put_value(u16 value){ gpio_value=value; } u16 get_value(){ return gpio_value; } bool put_pin(u16 pin){ int size=0; if(gpio_pin_x==NULL){ GET_ARRAY_LEN(pin_keywords,size); if(pin<size){ gpio_pin_x = pin_keywords[pin].token; printf ("------P");printf ("%d\r\n",pin); return TRUE; } } return FALSE; } u16 get_pin(){ return gpio_pin_x; } bool put_gpio(char* p){ int i=0; int size=0; if(gpio_x==NULL){ GET_ARRAY_LEN(gpio_keywords,size); for (i=0; i<size; i++) { gpio_kt = gpio_keywords[i]; if (strncasecmp(p, gpio_kt.keyword, strlen(p)) == 0) { printf ("------");printf ("%s\r\n",gpio_kt.keyword); gpio_x = gpio_kt.token; return TRUE; } } } return FALSE; } GPIO_TypeDef * get_gpio(){ return gpio_x; } bool write_gpio(){ if(gpio_x== GPIOA && (gpio_pin_x==GPIO_Pin_9 || gpio_pin_x==GPIO_Pin_10)){ return FALSE; } GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = gpio_pin_x; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(gpio_x , &GPIO_InitStructure); GPIO_WriteBit(gpio_x , gpio_pin_x,(BitAction)gpio_value); return TRUE; }
总结
以上是生活随笔为你收集整理的Basic脚本解释器移植到STM32的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。