eew_Violet 发表于 2021-4-18 23:31

【雅特力开发板 AT32F421 测评】SPI使用

本帖最后由 eew_Violet 于 2021-4-18 23:38 编辑

<p><span style="font-size:24px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在使用国产替代时有一点需要格外注意,大家看下面的框图,该器件的APB1和APB2均可以满频运行,而S*M的APB1是2分频的,有时候移植过来之后发现原来可以的,现在不行了,会怀疑自己哪里漏掉了什么,有时候检查一下APB1的频率会有意外收获哦.</span></p>

<p><span style="font-size:24px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这里我们看到SPI2是挂在APB1上的,所以,不用多说,找CMSIS下system_at32f4xx.c中,这里我们可以看到他的时钟配置,另外system_at32f4xx.c中已经写好了诸多时钟配置,需要哪个直接define就好.</span></p>

<p></p>

<p><span style="font-size:24px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;接下来可以着手编写SPI初始化函数了,这里我写到了at32_board.c中,</span></p>

<pre>
<code>void SPI2_Init()
{
GPIO_InitType GPIO_InitStructure;
SPI_InitTypeSPI_InitStructure;
       
        /* 启用时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_SPI2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_GPIOB , ENABLE);
       

/* 配置 SPI2引脚:
       *        SPI2_CLK--&gt;PB13
       *        SPI2_MOSI--&gt;PB15
       *        SPI2_NSS--&gt;PB12
       *        LCD_RS--&gt;PB14
       *        LCD_DC--&gt;PB11
       */
GPIO_StructInit(&amp;GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_13| GPIO_Pins_15;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_OutType = GPIO_OutType_PP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_NOPULL;
GPIO_Init(GPIOB, &amp;GPIO_InitStructure);
       
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_11|GPIO_Pins_12|GPIO_Pins_14;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_OutType = GPIO_OutType_PP;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOB, &amp;GPIO_InitStructure);
       
GPIO_PinAFConfig(GPIOB, GPIO_PinsSource13, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinsSource15, GPIO_AF_0);

/* SPI2配置*/
SPI_DefaultInitParaConfig(&amp;SPI_InitStructure);
SPI_InitStructure.SPI_TransMode = SPI_TRANSMODE_TX_HALFDUPLEX   ;
SPI_InitStructure.SPI_Mode = SPI_MODE_MASTER;
SPI_InitStructure.SPI_FrameSize = SPI_FRAMESIZE_8BIT;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_LOW;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1EDGE;
SPI_InitStructure.SPI_NSSSEL = SPI_NSSSEL_SOFT;
SPI_InitStructure.SPI_MCLKP = SPI_MCLKP_2;
SPI_InitStructure.SPI_FirstBit = SPI_FIRSTBIT_MSB;
SPI_InitStructure.SPI_CPOLY = 7;
SPI_Init(SPI2, &amp;SPI_InitStructure);
       
        SPI_Enable(SPI2, ENABLE);
}

</code></pre>

<p><span style="font-size:24px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;写法的话,很熟悉了,还有一个注意点,看似一样实际大小写和加不加s后缀还是有区别的,在配置成员之前会调用GPIO_StructInit();和SPI_DefaultInitParaConfig();函数,用来填充默认值,有时候我们并不一定会配置每个成员,所以配置之前先恢复下默认,是个好习惯.下面就是要把外设的驱动代码加入工程中去,</span></p>

<p><span style="font-size:24px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.c和.h已经加入工程,下面要改写oled.h中的头文件包含和要用的IO修改,</span><span style="font-size:24px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;到这基本就完成了,编译下没报错就可以写main了,这里我自己取了下模</span></p>

<pre>
<code>unsigned char COAlarm16={


{0x00,0xC2,0xBA,0x82,0x82,0xFE,0xA2,0x90,0xFC,0x4B,0x48,0xF9,0x4A,0x48,0x08,0x00},
{0x20,0x10,0x08,0x46,0x81,0x7F,0x00,0x00,0xFF,0x22,0x22,0x3F,0x22,0x22,0x20,0x00},/*"雅",0*/
/* (16 X 16 , 宋体 )*/

{0x40,0x3C,0x10,0xFF,0x10,0x10,0x40,0x48,0x48,0x48,0x7F,0x48,0xC8,0x48,0x40,0x00},
{0x02,0x06,0x02,0xFF,0x01,0x01,0x00,0x02,0x0A,0x12,0x42,0x82,0x7F,0x02,0x02,0x00},/*"特",1*/
/* (16 X 16 , 宋体 )*/

{0x00,0x10,0x10,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x10,0x10,0xF0,0x00,0x00,0x00},
{0x00,0x80,0x40,0x20,0x18,0x06,0x01,0x00,0x20,0x40,0x80,0x40,0x3F,0x00,0x00,0x00},/*"力",2*/
/* (16 X 16 , 宋体 )*/

{0x24,0x24,0xA4,0xFE,0xA3,0x22,0x00,0x22,0xCC,0x00,0x00,0xFF,0x00,0x00,0x00,0x00},
{0x08,0x06,0x01,0xFF,0x00,0x01,0x04,0x04,0x04,0x04,0x04,0xFF,0x02,0x02,0x02,0x00},/*"科",3*/
/* (16 X 16 , 宋体 )*/

{0x10,0x10,0x10,0xFF,0x10,0x90,0x08,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x08,0x00},
{0x04,0x44,0x82,0x7F,0x01,0x80,0x80,0x40,0x43,0x2C,0x10,0x28,0x46,0x81,0x80,0x00},/*"技",4*/
/* (16 X 16 , 宋体 )*/

};</code></pre>

<p>最后补上main</p>

<pre>
<code>int main(void)
{   


while (1)
{
    OLED_ShowString(30,0,(uint8_t*)"Arterytek",16);
               
                OLED_ShowChinese(11,26,0,16);//雅
                OLED_ShowChinese(33,26,1,16);//特
                OLED_ShowChinese(55,26,2,16);//力
                OLED_ShowChinese(77,26,3,16);//科
                OLED_ShowChinese(99,26,4,16);//技

                OLED_ShowNum(12,50,86,2,12);//电话
                OLED_ShowChar(26,50,'-',12);
                OLED_ShowNum(36,50,23,2,12);
                OLED_ShowChar(50,50,'-',12);
                OLED_ShowNum(60,50,6868,4,12);
                OLED_ShowNum(92,50,8899,4,12);
                OLED_Refresh();
                return 0;
               
               
}
}
</code></pre>

<p><span style="font-size:24px;">&nbsp; &nbsp; &nbsp; &nbsp;还要把SPI2_Init和OLED_Init,&nbsp;放在rt_hw_board_init中,这样系统启动时候就会把他一并初始化.</span><span style="font-size:24px;">烧录下,</span></p>

<p><span style="font-size:24px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;说明SPI已经正常上班了.</span></p>

Jacktang 发表于 2021-4-19 09:50

<p>写SPI初始化函数时,写到到了at32_board.c中,这个手册中有没有介绍</p>

freebsder 发表于 2021-4-19 22:52

<p>谢谢分享,期待后续!</p>

eew_Violet 发表于 2021-4-19 23:18

Jacktang 发表于 2021-4-19 09:50
写SPI初始化函数时,写到到了at32_board.c中,这个手册中有没有介绍

<p>spi初始函数写哪都行的,只要在rt_board_init中调用初始化即可,</p>

火辣西米秀 发表于 2021-4-19 23:24

<p>时钟配置看着还是挺简单的,楼主威武</p>
页: [1]
查看完整版本: 【雅特力开发板 AT32F421 测评】SPI使用