Stm32 serial sampling and stepper motor control program of the instrument (main program and serial program, need to interrupt the experiment program template with the punctual atom timer)

Source: Internet
Author: User
Tags file handling
# Include <stm32f10x_lib.h> # include "sys. H "# include" usart. H "# include" delay. H "# include" LED. H "# include" key. H "# include" exti. H "# include" WDG. H "# include" timer. H "// stm32 serial sampling and meter stepper motor control program code // main program // Xu Ming // 2011.7 /*************** **************************************** * *******************/u8 len_out, flag, verify_1, verify_0; u8 ret_val1, ret_val2, ret_val3; u8 in_buf [12]; u8 out_buf [7] = {0x7f, 0x05, 0x01,0x01, 0x00,0x00,0x86}; u8 in_sampling_buf [9] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; u8 defaults [15] = {0x7f, 0x0d, 0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x8e }; /*************************************** * **********************************/void sampling_verify (void) // calculate the verification value of the sample data {u8 I, j; 2010verify; verify_1 = 0; verify_0 = 0; Verify = 0; for (I = 0; I <9; I ++) {out_sampling_buf [I + 4] = in _ Sampling_buf [I] ;}for (j = 0; j <13; j ++) {verify = verify + out_sampling_buf [J];} out_sampling_buf [14] = verify & 0xff; out_sampling_buf [13] = verify> 8 ;} /*************************************** * **********************************/void send_sbu (u8 D) {usart1-> DR = D; while (usart1-> Sr & 0x40) = 0 ); // wait until sending ends }/********************************* **************************************** **/void send_sampling (void) {u8 I; for (I = 0; I <15; I ++) {send_sbu (out_sampling_buf [I]) ;}} /*************************************** * **********************************/void send_code (void) {u8 I; for (I = 0; I <7; I ++) {send_sbu (out_buf [I]) ;}} /*************************************** * **********************************/void send_invain (void) {out_buf [4] = 0x01; out_buf [6] = 0x87; send_code ();} /********************************** **************************************** **/Void send_right (void) {out_buf [4] = 0x00; out_buf [6] = 0x86; send_code ();} /*************************************** * ***********************************/void send_error (void) {out_buf [4] = 0x02; out_buf [6] = 0x88; send_code ();} /*************************************** * ***********************************/u8 compare_data (ret_val3) /// data comparison function {2010incre1, in Cre2; incre1 = in_buf [5] * 256 + in_buf [6]; incre2 = in_buf [8] * 256 + in_buf [9]; If (incre1 <300) & (incre2 <300) {return 0 ;}else {return 0xff ;}} /*************************************** * *********************************/u8 data_verify (ret_val2) // data verification function {u8 I, verify1, verify2; verify1 = 0; verify2 = 0; for (I = 0; I <Len; I ++) {verify1 = verify1 + in_buf [I];} verify2 = in_buf [Len] * 256 + in_buf [Len + 1]; If (verify1 = Verify2) {return 0;} else {return 0xff ;}} /*************************************** * ***********************************/u8 data_dispose (ret_val1) // data processing function {u8 I; for (I = 0; I <Len + 2; I ++) {in_buf [I] = usart_rx_buf [I];} if (data_verify (ret_val2) = 0) {If (in_buf [3] = 0x01) {sampling_verify (); send_sampling ();} if (in_buf [3] = 0x02) {If (compare_data (ret_val3) = 0) {send_right (); flag = 1; return 0;} else {send_in Vain (); Return 0xff ;}} else {send_error (); Return 0xff ;}} /*************************************** * **********************************/void step_moto (void) // stepper motor driver {2010plu_num1, plu_num2; plu_num1 = 0; plu_num2 = 0; num = 0, // init_direct (); plu_num1 = (in_buf [5] * 256 + in_buf [6]) * 24; // corner increment 1 plu_num2 = (in_buf [8] * 256 + in_buf [9]) * 24; // corner increment 2 while (plu_num1! = 0 | plu_num2! = 0) {tim3-> Dier | = 1 <0; // enable update interruption tim3-> Dier | = 1 <6; // enable trigger interruption tim3-> CR1 | = 0x01; // enable timer 3 if (num = 5) // 0.25 ms, 4,000 PLU/sec {num = 0; If (plu_num1> 0) {// pluse_1 = ~ Pluse_1; led0 =! Led0; plu_num1 --;} If (plu_num2> 0) {// pluse_2 = ~ Pluse_2; led1 =! Led1; plu_num2 -- ;}}} tim3-> Dier | = 0 <0; // update interruption tim3-> Dier | = 0 <6; // The tim3-> CR1 & = 0xfe; // close the timer 3 }/********************************* * *************************/INT main (void) {times = 0; stm32_clock_init (9); // set the system clock to delay_init (72); // initialize the uart_init (); // initialize the serial port to 9600led_init (); // initialize the hardware interface timerx_init (500,719) connected to the LED. // The Count frequency of 10 kHz. The Count frequency is 5000 ms, and the count frequency of 5 is 500 US. // if you want the count cycle of 50 us, you can set this to (5,719) while (1) {If (usart_rx_sta & 0x80) {If (data_dispose (ret_val1) = 0) & (flag = 1) {// led1 =! Led1; step_moto (); flag = 0;} usart_rx_sta = 0;} else {// times ++; // If (times % 30 = 0) // {// led0 =! Led0; //} // delay_ms (10 );}}}
# Include "timer. H "# include" LED. H "// mini stm32 Development Board // universal timer driver code // punctual atom @ alientek // 2010/6/1 u8 num; // timer 3 interrupt service program // 2 ms interrupt 1 void tim3_irqhandler (void) {If (tim3-> Sr & 0x0001) // overflow interrupt {num = num + 1;} tim3-> Sr & = ~ (1 <0); // clear the interrupt flag} // universal timer interrupt initialization // here, the clock is twice that of apb1, and apb1 is 36 m // arr: automatically reinstalls the value. // PSC: clock pre-minute frequency // here the timer 3 is used! Void timerx_init (2010arr, 2010psc) {RCC-> apb1enr | = 1 <1; // tim3 clock enabling tim3-> arr = arr; // set the counter auto-Reload value // just 1 ms tim3-> PSC = PSC; // pre-divider 7200, get a 10 kHz counting clock // these two things must be set at the same time to use the interrupt // tim3-> Dier | = 1 <0; // update interrupt allowed // tim3-> Dier | = 1 <6; // trigger interrupt allowed /// tim3-> CR1 | = 0x01; // enable timer 3 my_nvic_init (1, 3, tim3_irqchannel, 2); // preemption 1, Sub-Priority 3, group 2}
# Include "sys. H "# include" usart. H "// stm32 serial port sampling and meter stepper motor control program code // serial port program // Xu Ming // 2011.7 // v1.3 // supports Serial Port baud rate setting at different frequencies. // added support for printf // added the serial port command reception function. //////////////////////////////////////// /// // Add the following code, supports printf functions without selecting use microlib # If 1 # pragma import (_ use_no_semihosting) // The supported functions required by the standard library struct _ file {int handle; /* whatever you require here. if the only file you are using is * // * standard out Put using printf () for debugging, no file handling * // * is required. * //};/* file is typedef 'd in stdio. h. */file _ stdout; // DEFINE _ sys_exit () to avoid the use of semi-Host Mode _ sys_exit (int x) {x = x ;} // redefine the fputc function int fputc (int ch, file * f) {usart1-> DR = (u8) ch; while (usart1-> Sr & 0x40) = 0); // sends the message cyclically until the return ch is sent ;} # endif/end /////////////////////////////////// /// // # ifdef en_usart#rx // If Enable reception // Serial Port 1 to interrupt the service program // note that reading usartx-> SR can avoid inexplicable errors u8 usart_rx_buf [64]; // receive the buffer, up to 64 bytes. // receiving status // bit7, receiving completion mark // bit6, received 0x0d // bit5 ~ 0, the number of valid bytes received u8 usart_rx_sta = 0; // The receiving status mark u8 Len; void usart=irqhandler (void) {u8 res; if (usart1-> Sr & (1 <5) // receives data {res = usart1-> Dr; If (RES = 0x7f) {usart_rx_sta = 0 ;} if (usart_rx_sta & 0x80) = 0) // The receipt is incomplete {usart_rx_buf [usart_rx_sta] = res; If (usart_rx_buf [0] = 0x7f) {usart_rx_sta ++; if (usart_rx_sta = (usart_rx_buf [1] + 2) {Len = usart_rx_buf [1]; usart_rx_sta | = 0x80 ;} else {If (usart_rx_sta> (usart_rx_buf [1] + 2) {usart_rx_sta = 0 ;}} else usart_rx_sta = 0 ;}}# endif // initialize the IO Serial Port 1 // pclk2: pclk2 clock frequency (MHz) // bound: baud Rate // check OK // 091209 void uart_init (u32 pclk2, u32 bound) {float temp; 2010mantissa; 2010fraction; temp = (float) (pclk2 * 1000000) /(bound * 16); // obtain usartdivmantissa = temp; // obtain the integer part fraction = (temp-mantissa) * 16; // obtain the fractional part mantissa <= 4; mantissa + = fraction; RCC-> apb2enr | = 1 <2; // enable RCC at the Porta port-> apb2enr | = 1 <14; // enable serial clock gpioa-> CRH = 0x4444 44b4; // set the IO status to RCC-> apb2rstr | = 1 <14; // reset the serial port to 1rcc-> apb2rstr & = ~ (1 <14); // stop resetting // set the baud rate to usart1-> BRR = mantissa; // set the baud rate to usart1-> CR1 | = 0x200c; // 1-bit stop, no checkpoint. # ifdef en_usartjavasrx // If the receiver is enabled, // enable the receiver to interrupt usart1-> CR1 | = 1 <8; // PE interrupt enabling usart1-> CR1 | = 1 <5; // receive buffer non-air disconnection enabling my_nvic_init (3,3, usart?irqchannel, 2); // group 2, lowest priority # endif}
# Ifndef _ usart_h # DEFINE _ usart_h # include <stm32f10x_lib.h> # include "stdio. H "// mini stm32 Development Board // Serial Port 1 initialization // punctual atom @ alientek // 2010/5/27 extern u8 usart_rx_buf [64]; // receiving buffer, up to 63 bytes. the last byte is the line break extern u8 usart_rx_sta, Len; // The receipt status flag # define en_usart1_rx // enable Serial Port 1 to receive void uart_init (u32 pclk2, u32 bound); # endif
# Ifndef _ timer_h # DEFINE _ timer_h # include "sys. H "// mini stm32 Development Board // timer driver code // punctual atom @ alientek // 2010/6/1 extern u8 num; void timerx_init (2010arr, 2010psc); # endif
# Ifndef _ led_h # DEFINE _ led_h # include "sys. H "// mini stm32 Development Board // LED driver code // punctual atom @ alientek // 2010/5/27 // led port definition # define led0 paout (8) // pa8 # define led1 pdout (2) // pd2void led_init (void); // initialization # endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.