找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1067|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

單片機(jī)新手例程序/STM32開(kāi)門(mén)狗實(shí)驗(yàn)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:549735 發(fā)表于 2019-5-28 22:47 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
/*  */

#include "public.h"
#define TXD GPIO_Pin_9
#define RXD GPIO_Pin_10

void GPIOINIT()           //???B?????
{
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Pin=TXD;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_Init(GPIOA,&GPIO_InitStructure);

        GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable , ENABLE);

        GPIO_InitStructure.GPIO_Pin=RXD;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
        GPIO_Init(GPIOA,&GPIO_InitStructure);        
}
void RCCINIT()         //?????????
{        
        SystemInit();
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);        //?????ù???????
}
void USARTINIT()
{
        USART_InitTypeDef USART_InitStructure;
        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_Tx | USART_Mode_Rx;
        USART_Init(USART1, &USART_InitStructure);

        USART_Cmd(USART1,ENABLE);
        USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
        USART_ClearFlag(USART1,USART_FLAG_TC);
}
void NVICINIT()
{
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);                        
}
void IWDGINIT()
{
        IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//?????????д????
        IWDG_SetPrescaler(IWDG_Prescaler_64);//????IWDG?????//40K/64=1.6ms
        IWDG_SetReload(800);//????IWDG??????  12λ???0~4095??//800*1.6ms=1.28s
        IWDG_ReloadCounter();//????IWDG?????????????????IWDG??????
        IWDG_Enable();//???IWDG
}
int fputc(int ch,FILE *p)
{
        USART_SendData(USART1,(u8)ch);
        while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
        return ch;
}
int main()
{
        RCCINIT();
        GPIOINIT();
        USARTINIT();
        NVICINIT();
        IWDGINIT();
        printf("??????????????\r\n");
        printf("88888888888\r\n");
        while(1)
        {
                IWDG_ReloadCounter();
                printf("????ι????\r\n");        
                delayms(500);                        
        }               
}
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:509721 發(fā)表于 2019-5-29 11:39 | 只看該作者
函數(shù)命名很不規(guī)范,delay是怎么實(shí)現(xiàn)的的,是for循環(huán)空轉(zhuǎn)耗時(shí)這種方式實(shí)現(xiàn)的么,如果是,不建議這樣,學(xué)會(huì)利用定時(shí)器來(lái)實(shí)現(xiàn)延時(shí),郭天祥的延時(shí)耽誤了幾代人
回復(fù)

使用道具 舉報(bào)

板凳
ID:549735 發(fā)表于 2019-5-29 23:26 | 只看該作者
andyf626 發(fā)表于 2019-5-29 11:39
函數(shù)命名很不規(guī)范,delay是怎么實(shí)現(xiàn)的的,是for循環(huán)空轉(zhuǎn)耗時(shí)這種方式實(shí)現(xiàn)的么,如果是,不建議這樣,學(xué)會(huì)利 ...

謝謝指教,延時(shí)方面確實(shí)不精
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表