安裝arduino2.00以上版本后,運行一次,把這個文件放在C:\Users\Administrator\AppData\Local下,就可以驅動STM32F030 STM32F103系列單片機,開發效率遠高于keill,
下載路徑:鏈接:https://pan.baidu.com/s/1eT9Zsy11HqAp5Yr37yRqKg
提取碼:lfs5
b230802
#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \
defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \
defined(STM32WB) || defined(STM32MP1) )
#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting.
#endif
#define TIMER_INTERRUPT_DEBUG 0
#define _TIMERINTERRUPT_LOGLEVEL_ 0
#include "STM32TimerInterrupt.h"
#define bpm1_h digitalWrite(PA15, HIGH); // Pin 33/PB0 control on-board LED_GREEN on F767ZI
#define bpm1_l digitalWrite(PA15, LOW);
#define bpm2_h digitalWrite(PB3, HIGH); // Pin 33/PB0 control on-board LED_GREEN on F767ZI
#define bpm2_l digitalWrite(PB3, LOW);
#ifndef LED_BUILTIN
#define LED_BUILTIN PB5 // Pin 33/PB0 control on-board LED_GREEN on F767ZI
#endif
#ifndef LED_BLUE
#define LED_BLUE PC13 // Pin 73/PB7 control on-board LED_BLUE on F767ZI
#endif
#ifndef LED_RED
#define LED_RED PB5 // Pin 74/PB14 control on-board LED_BLUE on F767ZI
#endif
#include "STM32TimerInterrupt.h"
#include "STM32_ISR_Timer.h"
#define TIMER_INTERVAL_MS 10
#define HW_TIMER_INTERVAL_MS 10
void sub9(void) ;
#define u8 unsigned char
u8 Rxdata[10] ;
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
boolean bfbase4ms = false; ;
boolean bfdatasend = false ;
int inByte; // Byte read from Serial1
unsigned int wcad ;
union{
struct{
unsigned bfbase : 1 ;
unsigned syi893 : 1 ;
unsigned bfpoweroff : 1 ;
unsigned shu164 :1;
unsigned bfrf_isrread : 1 ;
}onebit ;
unsigned char allbits ;
}wflag ;
// Depending on the board, you can select STM32 Hardware Timer from TIM1-TIM22
// For example, F767ZI can select Timer from TIM1-TIM14
// If you select a Timer not correctly, you'll get a message from ci[ompiler
// 'TIMxx' was not declared in this scope; did you mean 'TIMyy'?
// Init STM32 timer TIM1
STM32Timer ITimer(TIM1);
// Init STM32_ISR_Timer
// Each STM32_ISR_Timer can service 16 different ISR-based timers
STM32_ISR_Timer ISR_Timer;
#define TIMER_INTERVAL_0_5S 500L
#define TIMER_INTERVAL_100mS 5 //100ms
#define TIMER_INTERVAL_1_5S 1500L
void TimerHandler()
{
ISR_Timer.run();
}
// In STM32, avoid doing something fancy in ISR, for example complex Serial.print with String() argument
// The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
// Or you can get this run-time error / crash
void doingSomething1()
{
// digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
void doingSomething2()
{
// digitalWrite(LED_BLUE, !digitalRead(LED_BLUE));
bfbase4ms = true ;
wflag.onebit.bfbase = 1 ;
// Serial2.write(0x5a);
}
void doingSomething3()
{
digitalWrite(LED_RED, !digitalRead(LED_RED));
}
void subled(void)
{
u8 static wcled ;
wcled++ ;
if(wcled>3)
{
wcled = 0 ;
sub9();
}
}
const unsigned char Bank_Reg[]={
0x33, //0
0x00,
0x01,
0x03,
0x01,
66,
0x26, //1Mbps data rate, output power=-5dBm
0x07,
0x00,
0x00,
0x00,
0x00,
0xc3,
0xc4,
0xc5,
0xc6,
0x20,
0x20,
0x20,
0x20,
0x20,
0x00,
};
void subdelay(unsigned int settime)
{
unsigned int i ;
i = 0 ;
while(i<settime)
{
i++ ;
}
}
#define MISO digitalRead(PA6)
#define IRQ digitalRead(PA0)
#define M0SI0 digitalWrite(PA7, LOW);
#define M0SI1 digitalWrite(PA7, HIGH);
#define SCK0 digitalWrite(PA5, LOW);
#define SCK1 digitalWrite(PA5, HIGH);
#define CE0 digitalWrite(PB0, LOW);
#define CE1 digitalWrite(PB0, HIGH);
#define CSN0 digitalWrite(PA4, LOW);
#define CSN1 digitalWrite(PA4, HIGH);
void SPI_GPIO_Init()
{
pinMode(PA6, INPUT_PULLUP);
pinMode(PA7, OUTPUT);
pinMode(PA5, OUTPUT);
pinMode(PA4, OUTPUT);
pinMode(PB0, OUTPUT);
pinMode(PA0, INPUT_PULLUP);
pinMode(PB3, OUTPUT);
pinMode(PA15, OUTPUT);
pinMode(PB5, OUTPUT);
}
unsigned char sub1(unsigned char value)
{
u8 racc0 ;
for(racc0=0;racc0<8;racc0++) // output 8-bit
{
if(value & 0x80){M0SI1;}
else{M0SI0; }
value = (value << 1); // shift next bit into MSB..
SCK1; // Set SCK high..
if(MISO){value = value|0x01 ;}
else{value = value&0xfe ;}
SCK0;
}
return(value);
}
void sub2(unsigned char reg, unsigned char value)
{
CSN0 ; // CSN low, init SPI transaction
sub1(reg|0x20); // select register
sub1(value); // ..and write value to it..
CSN1; // CSN high again
}
void sub3(unsigned char reg, unsigned char value)
{
CSN0; // CSN low, init SPI transaction
sub1(reg); // select register
sub1(value); // ..and write value to it..
CSN1; // CSN high again
}
unsigned char sub4(unsigned char reg)
{
unsigned char value,op_status;
CSN0 ; // CSN low, initialize SPI communication...
op_status=sub1(reg); // Select register to read from..
value = sub1(0); // ..then read register value
CSN1; // CSN high, terminate SPI communication
return(value); // return register value
}
void sub5(void)
{
u8 racc0 ;
racc0=sub4(7);
racc0=racc0&0x80;
if(racc0 == 0x80){sub3(0x50,0x53); }
}
void sub6(void)
{
CSN0 ;
sub1(0x0a|0x20);
sub1(0Xd2);
sub1(0Xb5);
sub1(0x99);
sub1(0xb3);
sub1(0x41);
CSN1 ;
subdelay(10);
CSN0 ;
sub1(0x10|0x20);
sub1(0Xd2);
sub1(0Xb5);
sub1(0x99);
sub1(0xb3);
sub1(0x41);
CSN1 ;
// sub2(0x02,0x01);
}
void sub7(void)
{
unsigned char byte_ctr,racc0 ;
CE0;
sub5();
sub6();
subdelay(10);
sub2(0xE2,0);//flush Tx
racc0=sub4(0x00); // read register 0x00's value
byte_ctr=racc0|0x01;//set bit 1
sub2( 0x00, byte_ctr); // Set PWR_UP bit, enable CRC(2 length) & Prim:RX. RX_DR enabled.
CE1;
}
void sub8(void)
{
unsigned char sendcount,tempdata ;
sendcount = 0 ;
sub5();
while(sendcount<22)
{
tempdata = Bank_Reg[sendcount];
sub2(sendcount,tempdata);
sendcount++;
subdelay(10);
}
sub3(0x50,0x73);
sub2(0x1c,0x3f);
sub2(0x1d,0x07);
subdelay(100);
sub7();
}
void sub9(void)
{
u8 racc0 ;
u8 static wcled ;
if(!IRQ)
{
CSN0 ;
subdelay(10);
sub1(0x61);
// digitalWrite(LED_BLUE, !digitalRead(LED_BLUE));
for(racc0=0 ; racc0<7;racc0++)
{
Rxdata[racc0] = sub1(0) ;
}
if((Rxdata[0]==0x55)&&(Rxdata[1]==0xaa)&&(Rxdata[3]==0x03))
{
wcled = 0 ;
if((Rxdata[4]==2))
{
bpm1_l;
bpm2_h ;
// digitalWrite(LED_BLUE, HIGH);
// digitalWrite(LED_RED, LOW);
}
else if((Rxdata[4]==1))
{
bpm1_h;
bpm2_l ;
// digitalWrite(LED_RED , HIGH);
// digitalWrite(LED_BLUE, LOW);
}
else
{
bpm1_h;
bpm2_h ;
// digitalWrite(LED_RED , LOW);
// digitalWrite(LED_BLUE, LOW);
}
}
CSN1 ;
sub2(0x07,0x70);
sub3(0xE2,0);
// Serial.print("RXOK\n");
}
else
{
wcled++ ;
if(wcled>10)
{
wcled = 0 ;
// digitalWrite(LED_BLUE, LOW);
}
}
}
void subpoweron(void)
{
u8 wccheck ;
wccheck = 0 ;
while(wccheck != 18)
{
sub2(0x05,18);
delay(50);
wccheck = sub4(0x05);
if(stringComplete==false)
{
// Serial2.write(84);
stringComplete = true ;
}
else
{
stringComplete=false ;
// Serial2.write(14);
}
}
}
void setup()
{
Serial.begin(9600);
delay(100);
Serial.print(F("\nStarting TimerInterruptLEDDemo on ")); Serial.println(BOARD_NAME);
Serial.println(STM32_TIMER_INTERRUPT_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Instantiate HardwareTimer object. Thanks to 'new' instanciation, HardwareTimer is not destructed when setup() function is finished.
//HardwareTimer *MyTim = new HardwareTimer(Instance);
// configure pin in output mode
pinMode(LED_BLUE, OUTPUT);
// Interval in microsecs
if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, TimerHandler))
{
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(millis());
}
else
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
// You can use up to 16 timer for each ISR_Timer
// ISR_Timer.setInterval(TIMER_INTERVAL_0_5S, doingSomething1);
// ISR_Timer.setInterval(TIMER_INTERVAL_1_5S, doingSomething3);
SPI_GPIO_Init();
subpoweron();
sub8();
ISR_Timer.setInterval(TIMER_INTERVAL_100mS, doingSomething2);
bpm1_l;
bpm2_l ;
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
stringComplete = true;
}
}
void loop()
{
/* Nothing to do all is done by hardware. Even no interrupt required. */
if(wflag.onebit.bfbase)
{
wflag.onebit.bfbase = 0 ;
subled();
wcad = analogRead(PA3); // read the input pin
if(wcad>30)
{
digitalWrite(LED_RED , LOW);
digitalWrite(LED_BLUE, HIGH );
}
else
{
digitalWrite(LED_RED , HIGH);
digitalWrite(LED_BLUE, LOW);
}
}
}
|