自己整理的STM32代碼_第1頁
自己整理的STM32代碼_第2頁
自己整理的STM32代碼_第3頁
自己整理的STM32代碼_第4頁
自己整理的STM32代碼_第5頁
已閱讀5頁,還剩16頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、用戶可通過多個預(yù)分頻器配置ahb、高速apb(apb2)和低速apb(apb1)域的頻率。ahb和apb2域的最大頻率是72mhz。apb1域的最大允許頻率是36mhz。rcc通過ahb時鐘8分頻后供給cortex系統(tǒng)定時器的外部時鐘。通過對systick控制與狀態(tài)寄存器的設(shè)置,可選擇上述時鐘或cortex ahb時鐘作為systick時鐘。adc時鐘由高速apb2時鐘經(jīng)2、4、6或8分頻后獲得。定時器時鐘頻率是其所在apb總線頻率的兩倍。然而,如果相應(yīng)的apb預(yù)分頻系數(shù)是1,定時器的時鐘頻率與所在apb總線頻率一致。fclk是cortex-m3的自由運行時鐘。詳情見arm的cortex-m3

2、技術(shù)參考手冊。定時器計算公式:(1+tim_prescaler )/72m)*(1+tim_period )=(1+7199)/72m)*(1+9999)=1秒 定時器初始化程序 pwm計算公式:輸出頻率=時鐘/(arr+1) 占空比=ccrx/arr x 100%void inittimer2_tim(void) tim_timebaseinittypedef tim_initstructure; nvic_inittypedef nvic_initstructure; nvic_initstructure.nvic_irqchannel = tim2_irqchannel; nvic_in

3、itstructure.nvic_irqchannelpreemptionpriority = 2; nvic_initstructure.nvic_irqchannelsubpriority = 1; nvic_initstructure.nvic_irqchannelcmd = enable; nvic_init(&nvic_initstructure); /配置時鐘 rcc_apb1periphclockcmd(rcc_apb1periph_tim2, enable); tim_cmd(tim2, disable); tim_initstructure.tim_period =

4、3600; /設(shè)置自動裝載寄存器,4ms tim_initstructure.tim_countermode = tim_countermode_up; /向上計數(shù) tim_initstructure.tim_prescaler = 79; /預(yù)分頻 tim_initstructure.tim_clockdivision = tim_ckd_div1; /輸入內(nèi)部時鐘頻率是否倍頻 /tim_initstructure.tim_repetitioncounter=0; tim_timebaseinit(tim2, &tim_initstructure); tim_itconfig(tim

5、2, tim_it_update, enable); /開定時中斷 tim_cmd(tim2, enable); /使能timer2 usart口初始化程序#include "all.h"/#define send_mode_dma/dma發(fā)送方式開關(guān)/#define usart1_use#define usart3_use#define usart1_rate 4800#define usart2_rate 4800#define usart3_rate 4800static void inithw_usart(void);/*初始化部分* *初始化硬件包括: *rcc初

6、始化 *gpio初始化 *nvic初始化 *配置初始化:包括使能、傳輸模式等 *變量初始化: *發(fā)送和接收緩存區(qū)初始化 */static void inithw_usart(void) usart_inittypedef usart_initstructure; gpio_inittypedef gpio_initstructure; nvic_inittypedef nvic_initstructure;#ifdef usart2_use /配置485收發(fā)控制io(默認接收,低電平) gpio_initstructure.gpio_pin = gpio_pin_0; gpio_initstr

7、ucture.gpio_speed = gpio_speed_50mhz; gpio_initstructure.gpio_mode = gpio_mode_out_pp; gpio_init(gpioa, &gpio_initstructure); gpio_resetbits(gpioa, gpio_pin_0); /使能時鐘 rcc_apb1periphclockcmd(rcc_apb1periph_usart2, enable); /中斷優(yōu)先級 nvic_initstructure.nvic_irqchannel = usart2_irqchannel; nvic_initst

8、ructure.nvic_irqchannelpreemptionpriority = 0; nvic_initstructure.nvic_irqchannelsubpriority = 0; nvic_initstructure.nvic_irqchannelcmd = enable; nvic_init(&nvic_initstructure); /配置接收管腳pa3 gpio_initstructure.gpio_pin = gpio_pin_3; gpio_initstructure.gpio_speed = gpio_speed_50mhz; gpio_initstruct

9、ure.gpio_mode = gpio_mode_in_floating; gpio_init(gpioa, &gpio_initstructure); /配置發(fā)送管腳pa2 gpio_initstructure.gpio_pin = gpio_pin_2; gpio_initstructure.gpio_speed = gpio_speed_50mhz; gpio_initstructure.gpio_mode = gpio_mode_af_pp; gpio_init(gpioa, &gpio_initstructure); /波特率、字長、停止位、奇偶校驗位、硬件流控制、

10、異步串口為默認(被屏蔽字設(shè)置) usart_initstructure.usart_baudrate = usart2_rate; 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_hardwareflowc

11、ontrol_none; usart_initstructure.usart_mode = usart_mode_rx | usart_mode_tx; usart_init(usart2, &usart_initstructure); /開接收中斷 usart_itconfig(usart2, usart_it_rxne, enable); /#ifdef send_mode_dma /發(fā)送啟用dma模式 / usart_dmacmd(usart2, usart_dmareq_tx, enable); #else /開發(fā)送中斷 usart_itconfig(usart2, usart

12、_it_tc, enable); /#endif /使能usart usart_cmd(usart2, enable);#endif #ifdef usart3_use /使能時鐘 rcc_apb1periphclockcmd(rcc_apb1periph_usart3, enable); /中斷優(yōu)先級 nvic_initstructure.nvic_irqchannel = usart3_irqchannel; nvic_initstructure.nvic_irqchannelpreemptionpriority = 0; nvic_initstructure.nvic_irqchanne

13、lsubpriority = 0; nvic_initstructure.nvic_irqchannelcmd = enable; nvic_init(&nvic_initstructure); /配置接收管腳pb11 gpio_initstructure.gpio_pin = gpio_pin_11; gpio_initstructure.gpio_speed = gpio_speed_50mhz; gpio_initstructure.gpio_mode = gpio_mode_in_floating; gpio_init(gpiob, &gpio_initstructur

14、e); /配置發(fā)送管腳pb10 gpio_initstructure.gpio_pin = gpio_pin_10; gpio_initstructure.gpio_speed = gpio_speed_50mhz; gpio_initstructure.gpio_mode = gpio_mode_af_pp; gpio_init(gpiob, &gpio_initstructure); /波特率、字長、停止位、奇偶校驗位、硬件流控制、異步串口為默認(被屏蔽字設(shè)置) usart_initstructure.usart_baudrate = usart3_rate; usart_init

15、structure.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;

16、 usart_init(usart3, &usart_initstructure); usart_cmd(usart3, enable);#endif /-u16 k=0;void init_usart(void) /此函數(shù)在sys_cfg()中初始化 inithw_usart();int main(void) /系統(tǒng)初始化 sys_cfg(); g_carderrcode=init_sd(); /sd檢測標志位,g_carderrcode=0表示sd卡正常 ,為g_carderrcode=1表示sd卡有問題 /*鐵電檢測程序*/ writedata_fram( us0, 0x1000

17、, 10, null, 0); delay1us(10000); readdata_fram( us1, 0x1000, 10,null, 0); delay1us(10000); readdata_fram( us1, 0x1000, 10,null, 0); /writesendbuf_usart(us0, 10, 3); gpio_setbits(gpioe, gpio_pin_10 | gpio_pin_11 | gpio_pin_12 | gpio_pin_13 | gpio_pin_14 | gpio_pin_15); rddisplay_led(); /數(shù)碼管顯示函數(shù) while

18、(1) usart_senddata(usart3, 0xbb); k=usart_receivedata(usart3); delay1us(10); k=0; 我用的是usart3,其余的usart口都一樣,在網(wǎng)上的代碼大多都不是很全,上述相關(guān)代碼是我自己測試時編寫的,拿過來就可以用,需要傳輸大量數(shù)據(jù)時自己做個函數(shù)就可以了。方便大家學(xué)習(xí)。stm32的spi配置程序(此程序只是個stm32的配置程序,沒有接外圍器件所以片選位可以用)static void inithw_spi(void) spi_inittypedef spi_initstructure; gpio_inittypedef

19、gpio_initstructure; nvic_inittypedef nvic_initstructure;#ifdef spi1_use /輸出時鐘 rcc_apb2periphclockcmd(rcc_apb2periph_gpioa | rcc_apb2periph_gpiob | rcc_apb2periph_spi1, enable); spi_cmd(spi1, disable); gpio_setbits(gpioa, gpio_pin_4); /中斷優(yōu)先級 nvic_initstructure.nvic_irqchannel = spi1_irqchannel; nvic_

20、initstructure.nvic_irqchannelpreemptionpriority = 3; nvic_initstructure.nvic_irqchannelsubpriority = 2; nvic_initstructure.nvic_irqchannelcmd = enable; nvic_init(&nvic_initstructure); /配置片選 gpio_initstructure.gpio_pin = gpio_pin_4; gpio_initstructure.gpio_speed = gpio_speed_50mhz; gpio_initstruc

21、ture.gpio_mode = gpio_mode_out_pp; gpio_init(gpioa, &gpio_initstructure); /配置3線制io gpio_initstructure.gpio_pin = gpio_pin_5 | gpio_pin_6 | gpio_pin_7; gpio_initstructure.gpio_speed = gpio_speed_50mhz; gpio_initstructure.gpio_mode = gpio_mode_af_pp; gpio_init(gpioa, &gpio_initstructure); /配置s

22、pi模式:主、雙向、8位、模式0、軟件片選(3線制)、18m波特率 spi_initstructure.spi_direction = spi_direction_2lines_fullduplex; spi_initstructure.spi_mode = spi_mode_master; spi_initstructure.spi_datasize = spi_datasize_8b; spi_initstructure.spi_cpol = spi_cpol_low; spi_initstructure.spi_cpha = spi_cpha_1edge; spi_initstructu

23、re.spi_nss = spi_nss_soft; spi_initstructure.spi_baudrateprescaler = spi_baudrateprescaler_8; spi_initstructure.spi_firstbit = spi_firstbit_msb; spi_initstructure.spi_crcpolynomial = 7; spi_init(spi1, &spi_initstructure); /無效片選 gpio_setbits(gpioa, gpio_pin_4); /禁止crc spi_calculatecrc(spi1, disab

24、le); /開接收中斷 /spi_itconfig(spi1, spi_it_rxne, enable); /使能spi spi_cmd(spi1, enable);#endif #ifdef spi2_use /輸出時鐘 rcc_apb1periphclockcmd(rcc_apb1periph_spi2, enable); /中斷優(yōu)先級 nvic_initstructure.nvic_irqchannel = spi2_irqchannel; nvic_initstructure.nvic_irqchannelpreemptionpriority = 3; nvic_initstructu

25、re.nvic_irqchannelsubpriority = 3; nvic_initstructure.nvic_irqchannelcmd = enable; nvic_init(&nvic_initstructure); /配置片選 gpio_initstructure.gpio_pin = gpio_pin_12; gpio_initstructure.gpio_speed = gpio_speed_50mhz; gpio_initstructure.gpio_mode = gpio_mode_out_pp; gpio_init(gpiob, &gpio_initst

26、ructure); /配置3線制io gpio_initstructure.gpio_pin = gpio_pin_13 | gpio_pin_14 | gpio_pin_15; gpio_initstructure.gpio_speed = gpio_speed_50mhz; gpio_initstructure.gpio_mode = gpio_mode_af_pp; gpio_init(gpiob, &gpio_initstructure); /配置spi模式:主、雙向、8位、模式3、軟件片選(3線制)、18m波特率 spi_initstructure.spi_direction

27、 = spi_direction_2lines_fullduplex; spi_initstructure.spi_mode = spi_mode_master; spi_initstructure.spi_datasize = spi_datasize_8b; spi_initstructure.spi_cpol = spi_cpol_low; spi_initstructure.spi_cpha = spi_cpha_1edge; spi_initstructure.spi_nss = spi_nss_soft; spi_initstructure.spi_baudrateprescale

28、r = spi_baudrateprescaler_4; spi_initstructure.spi_firstbit = spi_firstbit_msb; spi_initstructure.spi_crcpolynomial = 7; spi_init(spi2, &spi_initstructure); /片選 gpio_setbits(gpiob, gpio_pin_12); /禁止crc spi_calculatecrc(spi2, disable); /開接收中斷 / spi_itconfig(spi2, spi_it_rxne, enable); /使能spi spi_

29、cmd(spi2, enable);#endif pwm初始化/*/代碼在pa1引腳上產(chǎn)生一個10k左右 50%占空比的方波輸出/*/void tim_configuration(void)tim_timebaseinittypedef tim_timebasesturcture;tim_ocinittypedef tim_ocinitstructure;gpio_inittypedef gpio_initstructure;/*開啟gpioa時鐘*/rcc_apb2periphclockcmd(rcc_apb2periph_gpioa ,enable);/*開啟tim2時鐘*/rcc_apb

30、1periphclockcmd(rcc_apb1periph_tim2,enable);/*初始化pa1,tim2-ch2通道*/gpio_initstructure.gpio_pin = gpio_pin_1;gpio_initstructure.gpio_mode = gpio_mode_af_pp;gpio_initstructure.gpio_speed = gpio_speed_50mhz;gpio_init(gpioa,&gpio_initstructure);/*tim2基本初始化*/tim_timebasesturcture.tim_period = 512; /*周期

31、0ffff*/tim_timebasesturcture.tim_prescaler = 12; /*時鐘分頻*/tim_timebasesturcture.tim_clockdivision = 0; /*時鐘分割*/tim_timebasesturcture.tim_countermode = tim_countermode_up; /*模式*/tim_timebaseinit(tim2, &tim_timebasesturcture);/*pwm 通道 初始化*/tim_ocinitstructure.tim_ocmode = tim_ocmode_pwm1; /*工作狀態(tài)*/t

32、im_ocinitstructure.tim_outputstate = tim_outputstate_enable; /*設(shè)定為輸出,需要pwm輸出才需要這行代碼*/tim_ocinitstructure.tim_pulse = 256; /*占空長度 0 周期(max)*/tim_ocinitstructure.tim_ocpolarity = tim_ocpolarity_high; /*高電平*/tim_oc2init(tim2, &tim_ocinitstructure); /*通道初始化*/*開啟tim2模塊*/tim_cmd(tim2, enable);/*測溫程序(內(nèi)

33、部adc)內(nèi)部傳感器采集誤差比較大*/void adc_hardinit(void) adc_inittypedef adc_initstructure; rcc_apb2periphclockcmd(rcc_apb2periph_adc1, enable); /* adc1 configuration -*/ adc_initstructure.adc_mode = adc_mode_independent; adc_initstructure.adc_scanconvmode = enable; adc_initstructure.adc_continuousconvmode = enab

34、le; adc_initstructure.adc_externaltrigconv = adc_externaltrigconv_none; adc_initstructure.adc_dataalign = adc_dataalign_right; adc_initstructure.adc_nbrofchannel = 1; adc_init(adc1, &adc_initstructure); /* adc1 regular channel14 configuration */ adc_regularchannelconfig(adc1, adc_channel_16, 1,

35、adc_sampletime_239cycles5 ); /* enable adc1 */ adc_cmd(adc1, enable); /* enable adc1 reset calibaration register */ adc_resetcalibration(adc1); /* check the end of adc1 reset calibration register */ while(adc_getresetcalibrationstatus(adc1); /* start adc1 calibaration */ adc_startcalibration(adc1);

36、/* check the end of adc1 calibration */ while(adc_getcalibrationstatus(adc1);u16 adc_filter(void)u16 result=0;u8 i;for(i=16;i>0;i-) /delay2us(1); result += value;return result/16;/u8 adc_getchannel(u8 channel, u16* value) switch (channel) case 0x16: channel = adc_channel_16; adc_tempsensorvrefint

37、cmd(enable); break; case 0x17: channel = adc_channel_17; break; default: channel = adc_channel_16; break; /* adc1 regular channel14 configuration */ adc_regularchannelconfig(adc1, channel, 1, adc_sampletime_239cycles5); adc_softwarestartconvcmd(adc1, enable); delay2us(100); *value = adc_getconversio

38、nvalue(adc1); if(channel = 0x16) adc_tempsensorvrefintcmd(disable); return 0; vs8 temperature=0;temperature_detect() u16 adc; u16 wd; if(!adc_getchannel(0x16, &value) /*supported temperature range: 40 to 125 degrees precision: ± 1.5 °c */ /*temperature (in °c) = (v25 - vsense) / a

39、vg_slope + 25. avg_slope = 4.3 mv/°c v25 = 1.43 v注:這里應(yīng)用的全為典型值,在datasheet上可以查取。 */ adc=adc_filter(); wd = (1.43 - adc*3.3/4096)*1000/4.35 + 25; delay2us(10); /*stm32的18b20程序*/#include <stdio.h>#include "all.h"/* private define -*/ /rom操作指令/讀rom#define read_rom 0x33/匹配rom#define m

40、atch_rom 0x55/跳過rom#define skip_rom 0xcc/搜索rom#define search_rom 0xf0/告警搜索#define alarm_search 0xec /存儲器操作指令/寫暫存存儲器#define write_scratchpad 0x4e/讀暫存存儲器#define read_scratchpad 0xbe/復(fù)制暫存存儲器#define copy_scratchpad 0x48/溫度變換#define convert_temperature 0x44/重新調(diào)出#define recall_eprom 0xb8/讀電源#define read_p

41、ower_supply 0xb4 /* private macro -*/ u16 ds18b20_pin = (1<<6); /接ds18b20的引腳/#define ds18b20_pin (1<<2)#define dq(x) gpiod->odr = (gpiod->odr & ds18b20_pin) | (x ? ds18b20_pin : 0) /具體使用那個端口修改此處#define read_dq gpio_readinputdatabit(gpiod,ds18b20_pin) /也要修改 void us_delay (u16 us

42、) while(us-);void gpioint(void) /pass gpio_inittypedef gpio_initstructure; rcc_apb2periphclockcmd(rcc_apb2periph_gpiod,enable); /打開時鐘 gpio_initstructure.gpio_pin = gpio_pin_6; /選擇引腳 gpio_initstructure.gpio_mode = gpio_mode_out_od; gpio_initstructure.gpio_speed = gpio_speed_2mhz; gpio_init(gpiod,&

43、;gpio_initstructure); /* function name : dqreset* description :送一個復(fù)位脈沖(最少保持480s的低電平信號),然后釋放總線,mcu進入接收狀態(tài),ds1820 在探測到引腳上的上升沿后,延時1560s后,ds1820發(fā)出存在脈沖(60240s的低電平信號)。* input : none* output : none* return : none*/u8 dqreset(void) /pass u8 temp; dq(1); us_delay(10); dq(0); us_delay(3500);/ 延時約480us,保持總線電平為低,總線上的器件將被復(fù)位。 dq(1); / 發(fā)送復(fù)位脈沖后,釋放總線,讓總線處于空閑狀態(tài) us_delay(110); / 釋放總線后,以便從機ds18b20通過拉低總線來指示其是否在線,存在檢測高電平時間:1560us, us_delay(110); / 兩段延時是為了保證延時精確 temp = read_dq; us_delay(1000); / 存在檢測低電平時間:60240us,延時約140us dq(1); ret

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論