专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
STM32
经不住诱惑,今天开始玩STM8!麻烦过来人进来讲讲心得
2019-12-26 18:53
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
19492
77
77
1、买的STM8 value line discovery板刚到手,从最简单的STM8S003开始吧。
2、下载了STVD/COSMIC。COSMIC_32K坛里有和谐好的,不过要先下载原版安装。
3、争取今晚把LED点亮。
4、其实我很看好STM8L152,之后要测试一下耗电和ADC性能。
5、纯业余玩的,还没想好弄点啥……
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
76条回答
millwood0
1楼-- · 2019-12-29 14:13
I use a template + modules for pretty much all of my projects.
the following is for iar ewstm8.
here is the template for main.c
#include <iostm8.h>
#include "gpio.h"
//#include "tmr2.h" //we use tim2
//#include "tmr3.h" //we use tim3
//#include "tmr4.h" //we use tim3
//hardware configuration
//end hardware configuration
void mcu_init(void) {
}
int main( void ) {
mcu_init(); //reset the mcu
while (1) {
}
return 0;
}
复制代码
加载中...
millwood0
2楼-- · 2019-12-29 15:48
here is gpio.h
#ifndef __GPIO_H
#define __GPIO_H
//port-related macros for stm8s
//DEFAULT: OPEN DRAIN OUTPUT
//set Px_CR1 and clear Px_CR2 to turn to push_pull output
#define IO_SET(port, bits) port |= (bits) //set bits on port
#define IO_CLR(port, bits) port &=~(bits) //clear bits on port
#define IO_FLP(port, bits) port ^= (bits) //flip bits on port
#define IO_GET(port, bits) ((port) & (bits)) //return bits on port
#define IO_OUT(ddr, bits) ddr |= (bits) //set bits as output. stm8s: sets a bit to make it an output
#define IO_IN(ddr, bits) ddr &=~(bits) //set bits as input. stm8s: clears a bit to make it an input
#define NOP() asm("nop") //nop
#define NOP2() {NOP(); NOP();}
#define NOP4() {NOP2(); NOP2();}
#define NOP8() {NOP4(); NOP4();}
#define NOP16() {NOP8(); NOP8();}
#ifndef ei
#define ei() asm("rim") //enable all interrupts
#endif
#ifndef di
#define di() asm("sim") //disable all interrupts
#endif
#define __enable_interrupt() ei()
#define __disable_interrupt() di()
//fmaster = 16Mhz / 8 -> 2Mhz
//fcpu = fmaster / 1 -> 2Mhz
#define F_MASTER (16000000ul / 8) //fmaster -> default to hsi (16mhz), divided by 8
#define F_CPU (F_MASTER) //fcpu
//define ld1 on stm8sdiscovery board
#define LED1_PORT PD_ODR
#define LED1_DDR PD_DDR
#define LED1 (1<<0)
//#define LED1_ON() IO_CLR(LED1_PORT, LED1) //turn on the leds, assuming output mode
//#define LED1_OFF() IO_SET(LED1_PORT, LED1) //turn off the leds, assuming output mode
//#define LED1_FLP() IO_FLP(LED1_PORT, LED1) //flip leds
//void (*mcu_reset)(void) = 0x0000; //jump to 0x0000 -> software reset
//simple multiples
#define x1(val) (val) //multiply val by 1
#define x2(val) (((val) << 1)) //multiply val by 2
#define x3(val) (x2(val) + (val)) //multiply val by 3
#define x4(val) (((val) << 2)) //multiply val by 4
#define x5(val) (x4(val) + (val)) //multiply val by 5
#define x6(val) (x4(val) + x2(val)) //multiply val by 6
#define x7(val) (x6(val) + (val)) //multiply val by 7
#define x8(val) ((val) << 3) //multiply val by 8
#define x9(val) (x8(val) + (val)) //multiply val by 9
//multiples of 10s
#define x10(val) (x8(val) + x2(val)) //multiply val by 10
#define x100(val) (x10(x10(val))) //multiply val by 100
#define x1000(val) (x100(x10(val))) //multiply val by 1000
#define x1k(val) x1000(val) //multiply val by 1000
#define x10k(val) (x100(x100(val))) //multiply val by 10000
#define x20(val) (x2(x10(val)))
#define x30(val) (x3(x10(val)))
#define x40(val) (x4(x10(val)))
#define x50(val) (x5(x10(val)))
#define x60(val) (x6(x10(val)))
#define x70(val) (x7(x10(val)))
#define x80(val) (x8(x10(val)))
#define x90(val) (x9(x10(val)))
//multiples of 100s
#define x200(val) (x2(x100(val)))
#define x300(val) (x3(x100(val)))
#define x400(val) (x4(x100(val)))
#define x500(val) (x5(x100(val)))
#define x600(val) (x6(x100(val)))
#define x700(val) (x7(x100(val)))
#define x800(val) (x8(x100(val)))
#define x900(val) (x9(x100(val)))
//custom definitions
#define x34(val) (x30(val) + x4(val)) //multiply val by 34
#define x97(val) (x90(val) + x7(val)) //multiply val by 97x
#endif //__gpio_h_
复制代码
you don't need to have all of it. it predefines LED1 (on PD0).
as well as F_CPU (running at 16Mhz / 8).
加载中...
millwood0
3楼-- · 2019-12-29 21:13
to blink an led, you will need to introduce delay.
here is the software delay routines I use:
delay.h:
#ifndef __DELAY_H
#define __DELAY_H
void delay(unsigned char dly);
void delay_us(unsigned short us);
void delay_ms(unsigned short ms);
#endif //delay_h
复制代码
delay.c
//simple example
/* Includes ------------------------------------------------------------------*/
//#include <iostm8.h>
//#include <stdio.h> //because we used printf
#include "gpio.h" //we use _nop_(), F_CPU
#include "delay.h" //we use software delays
/* Global variables ----------------------------------------------------------*/
//cpu running at 2Mhz
//#define _DLY_MS 158 //delay for EWAVR (USB1286)
//#define _DLY_MS 40 //delay for WINAVR debug mode(USB1286, no optimization)
//#define _DLY_MS 158 //delay for WINAVR release (USB1286)
//#define _DLY_MS 120 //delay for C51
//#define _DLY_MS 329 //delay for avr
//#define _DLY_MS 160 //delay for PIC18F46K20 @ 4Mhz
//#define _DLY_MS 200 //delay for PIC16F886
//#define _DLY_MS 984 //delay for LPC2106
//#define _DLY_MS 226 //lpc1343 on keil
#define _DLY_MS 215 //stm8s@2Mhz
//delay routines
void delay(unsigned char dly) { //waste some cycle
while (dly--)
continue;
}
//delay_us(1000) = 10ms
//delay_us(1) = 10us
void delay_us(unsigned short us) { //delay a us
delay(us); //delay the lsb
us = us >> 8; //retain the msb
while (us--) delay(0xff); //delay the msb
}
void delay_ms(unsigned short ms) { //delay ms
while (ms--) delay_us(_DLY_MS * (F_CPU / 2000000ul));
}
复制代码
加载中...
millwood0
4楼-- · 2019-12-29 23:39
an led blinky would be something like this:
#include <iostm8.h>
#include "gpio.h"
#include "delay.h" //we use software delay
//#include "tmr2.h" //we use tim2
//#include "tmr3.h" //we use tim3
//#include "tmr4.h" //we use tim3
//hardware configuration
#define LED1_DLY 500 //led1 delay, in ms
//end hardware configuration
void mcu_init(void) {
IO_CLR(LED1_PORT, LED1); //clear led1
IO_OUT(LED1_DDR, LED1); //led1 as output
}
int main( void ) {
mcu_init(); //reset the mcu
while (1) {
delay_ms(LED1_DLY); //waste some time
IO_FLP(LED1_PORT, LED1); //flip led1
}
return 0;
}
复制代码
it should work once you have it downloaded onto your chip.
加载中...
n_mos
5楼-- · 2019-12-30 00:49
精彩回答 2 元偷偷看……
加载中...
hellosky
6楼-- · 2019-12-30 01:17
我也在学
加载中...
首页
上一页
3
4
5
6
7
8
9
10
下一页
最后一页
一周热门
更多
>
相关问题
STM32F4上I2C(在PROTEUS中模拟)调试不通的问题
6 个回答
芯片供应紧张,准备换个MCU,MM32L系列替换STM32L系列的怎么样?
7 个回答
STM32同时使用两个串口进行数据收发时数据丢包的问题
5 个回答
STM32F103串口通信死机问题
4 个回答
STM32WLE5CC连接SX1268在LoRa模式下能与 SX1278互通吗?
2 个回答
STM32开发板免费用活动
7 个回答
stm32 处理 DHT11占用太多时间,大家程序是怎么设计的
8 个回答
分享一个STM32单片机做的离线编程器代码
9 个回答
相关文章
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
×
关闭
采纳回答
向帮助了您的网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
STM32
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
关闭
您已邀请
15
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
the following is for iar ewstm8.
here is the template for main.c
- #include <iostm8.h>
- #include "gpio.h"
- //#include "tmr2.h" //we use tim2
- //#include "tmr3.h" //we use tim3
- //#include "tmr4.h" //we use tim3
- //hardware configuration
- //end hardware configuration
- void mcu_init(void) {
- }
- int main( void ) {
- mcu_init(); //reset the mcu
- while (1) {
- }
- return 0;
- }
复制代码
- #ifndef __GPIO_H
- #define __GPIO_H
- //port-related macros for stm8s
- //DEFAULT: OPEN DRAIN OUTPUT
- //set Px_CR1 and clear Px_CR2 to turn to push_pull output
- #define IO_SET(port, bits) port |= (bits) //set bits on port
- #define IO_CLR(port, bits) port &=~(bits) //clear bits on port
- #define IO_FLP(port, bits) port ^= (bits) //flip bits on port
- #define IO_GET(port, bits) ((port) & (bits)) //return bits on port
- #define IO_OUT(ddr, bits) ddr |= (bits) //set bits as output. stm8s: sets a bit to make it an output
- #define IO_IN(ddr, bits) ddr &=~(bits) //set bits as input. stm8s: clears a bit to make it an input
- #define NOP() asm("nop") //nop
- #define NOP2() {NOP(); NOP();}
- #define NOP4() {NOP2(); NOP2();}
- #define NOP8() {NOP4(); NOP4();}
- #define NOP16() {NOP8(); NOP8();}
- #ifndef ei
- #define ei() asm("rim") //enable all interrupts
- #endif
- #ifndef di
- #define di() asm("sim") //disable all interrupts
- #endif
- #define __enable_interrupt() ei()
- #define __disable_interrupt() di()
- //fmaster = 16Mhz / 8 -> 2Mhz
- //fcpu = fmaster / 1 -> 2Mhz
- #define F_MASTER (16000000ul / 8) //fmaster -> default to hsi (16mhz), divided by 8
- #define F_CPU (F_MASTER) //fcpu
- //define ld1 on stm8sdiscovery board
- #define LED1_PORT PD_ODR
- #define LED1_DDR PD_DDR
- #define LED1 (1<<0)
- //#define LED1_ON() IO_CLR(LED1_PORT, LED1) //turn on the leds, assuming output mode
- //#define LED1_OFF() IO_SET(LED1_PORT, LED1) //turn off the leds, assuming output mode
- //#define LED1_FLP() IO_FLP(LED1_PORT, LED1) //flip leds
- //void (*mcu_reset)(void) = 0x0000; //jump to 0x0000 -> software reset
- //simple multiples
- #define x1(val) (val) //multiply val by 1
- #define x2(val) (((val) << 1)) //multiply val by 2
- #define x3(val) (x2(val) + (val)) //multiply val by 3
- #define x4(val) (((val) << 2)) //multiply val by 4
- #define x5(val) (x4(val) + (val)) //multiply val by 5
- #define x6(val) (x4(val) + x2(val)) //multiply val by 6
- #define x7(val) (x6(val) + (val)) //multiply val by 7
- #define x8(val) ((val) << 3) //multiply val by 8
- #define x9(val) (x8(val) + (val)) //multiply val by 9
- //multiples of 10s
- #define x10(val) (x8(val) + x2(val)) //multiply val by 10
- #define x100(val) (x10(x10(val))) //multiply val by 100
- #define x1000(val) (x100(x10(val))) //multiply val by 1000
- #define x1k(val) x1000(val) //multiply val by 1000
- #define x10k(val) (x100(x100(val))) //multiply val by 10000
- #define x20(val) (x2(x10(val)))
- #define x30(val) (x3(x10(val)))
- #define x40(val) (x4(x10(val)))
- #define x50(val) (x5(x10(val)))
- #define x60(val) (x6(x10(val)))
- #define x70(val) (x7(x10(val)))
- #define x80(val) (x8(x10(val)))
- #define x90(val) (x9(x10(val)))
- //multiples of 100s
- #define x200(val) (x2(x100(val)))
- #define x300(val) (x3(x100(val)))
- #define x400(val) (x4(x100(val)))
- #define x500(val) (x5(x100(val)))
- #define x600(val) (x6(x100(val)))
- #define x700(val) (x7(x100(val)))
- #define x800(val) (x8(x100(val)))
- #define x900(val) (x9(x100(val)))
- //custom definitions
- #define x34(val) (x30(val) + x4(val)) //multiply val by 34
- #define x97(val) (x90(val) + x7(val)) //multiply val by 97x
- #endif //__gpio_h_
复制代码you don't need to have all of it. it predefines LED1 (on PD0).as well as F_CPU (running at 16Mhz / 8).
here is the software delay routines I use:
delay.h:
- #ifndef __DELAY_H
- #define __DELAY_H
- void delay(unsigned char dly);
- void delay_us(unsigned short us);
- void delay_ms(unsigned short ms);
- #endif //delay_h
复制代码delay.c
- //simple example
- /* Includes ------------------------------------------------------------------*/
- //#include <iostm8.h>
- //#include <stdio.h> //because we used printf
- #include "gpio.h" //we use _nop_(), F_CPU
- #include "delay.h" //we use software delays
- /* Global variables ----------------------------------------------------------*/
- //cpu running at 2Mhz
- //#define _DLY_MS 158 //delay for EWAVR (USB1286)
- //#define _DLY_MS 40 //delay for WINAVR debug mode(USB1286, no optimization)
- //#define _DLY_MS 158 //delay for WINAVR release (USB1286)
- //#define _DLY_MS 120 //delay for C51
- //#define _DLY_MS 329 //delay for avr
- //#define _DLY_MS 160 //delay for PIC18F46K20 @ 4Mhz
- //#define _DLY_MS 200 //delay for PIC16F886
- //#define _DLY_MS 984 //delay for LPC2106
- //#define _DLY_MS 226 //lpc1343 on keil
- #define _DLY_MS 215 //stm8s@2Mhz
- //delay routines
- void delay(unsigned char dly) { //waste some cycle
- while (dly--)
- continue;
- }
- //delay_us(1000) = 10ms
- //delay_us(1) = 10us
- void delay_us(unsigned short us) { //delay a us
- delay(us); //delay the lsb
- us = us >> 8; //retain the msb
- while (us--) delay(0xff); //delay the msb
- }
- void delay_ms(unsigned short ms) { //delay ms
- while (ms--) delay_us(_DLY_MS * (F_CPU / 2000000ul));
- }
复制代码
- #include <iostm8.h>
- #include "gpio.h"
- #include "delay.h" //we use software delay
- //#include "tmr2.h" //we use tim2
- //#include "tmr3.h" //we use tim3
- //#include "tmr4.h" //we use tim3
- //hardware configuration
- #define LED1_DLY 500 //led1 delay, in ms
- //end hardware configuration
- void mcu_init(void) {
- IO_CLR(LED1_PORT, LED1); //clear led1
- IO_OUT(LED1_DDR, LED1); //led1 as output
- }
- int main( void ) {
- mcu_init(); //reset the mcu
- while (1) {
- delay_ms(LED1_DLY); //waste some time
- IO_FLP(LED1_PORT, LED1); //flip led1
- }
- return 0;
- }
复制代码it should work once you have it downloaded onto your chip.一周热门 更多>