单片机学习笔记之4——新的单片机:TM4

///单片机学习笔记之4——新的单片机:TM4

单片机学习笔记之4——新的单片机:TM4

由于我参加了自控方向的电赛,而我们省的电赛又是由Ti公司赞助的,所以在比赛是使用Ti公司的单片机会有得分加成,这就使得我需要重新再学习一种新的单片机,虽然刚开始的时候内心是拒绝的,但是后来当接触到了Ti之后,发现这个公司的IDE以及库函数比我原来开发stm32的那一套工具好用多了,于是也渐渐开始能接受学习tm4这块板子了。

这是TI公司的 CCS8 IDE,感受一下这风格

在寒假期间,我把tm4的基础使用学习的差不多了,同时也自己构建了一些以我自己的代码风格为标准的tm4驱动库,这篇博文的主要目的也就是分享一下我自己写的这些库函数。排序比较随意,将就着看看吧……

代码使用的开发板型号:TM4C123GTX
单片机型号:TM4C123GH6PM

特别提示:由于不同种类的开发板及芯片型号的区别,请在移植时确保读懂代码,并确认代码与自己的库函数的兼容性,并根据需要修改端口配置等。如果不兼容的话,可能需要使用自己版本的库函数替换代码中的库函数,这一块请自行配置吧,应该不会太难的~
此外,有部分中断初测采用的是在中断向量表中注册的方式,所以并没有动态注册的代码,还请留意。

1.ADS模块

ADS模块,即数模转换模块,这个模块可以把电压信号转换为PWM波来供单片机读取,这个模块通常会与其他传感器连接,用于获取传感器的数值。我提供的这个ADS驱动库包含了单通道电压的读取。

//ads.h
#ifndef ADS_H

#define ADS_H

#define VOLTAGE_RESOLUTION                0.125  /* 满量程2.048V */
#define VOLTAGE_U0                        3300.0 /* mV */

void ADS_Init();
void Get_ADS_Value();

#endif
//ads.c
#include "ads.h"


#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "inc/hw_timer.h"
#include "inc/hw_types.h"
#include "inc/hw_ssi.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/ssi.h"
#include "driverlib/fpu.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "sys_driver/ADS118/ads1118.h"
#include "libraries/delay/delay.h"

// ads寄存器输出值
uint16_t ads_outcode;

// ads1118测得电压值 单位 uV
double ads_voltage;

uint16_t ads_voltage_mv;

float fVoltage0 = 0, fVoltage1 = 0;

void ADS_Init()
{
    ADS1118_Init();
}

void Get_ADS_Value()
{
    ADS1118_Configure(ADS1118_CONFIG_0);
    delay_ms(10);
    ADS1118_DataGetFloat(&fVoltage0);
    ADS1118_Configure(ADS1118_CONFIG_1);
    delay_ms(10);
    ADS1118_DataGetFloat(&fVoltage1);
}

2.延时函数

制作延时函数的库其实很简单,甚至严格地说没有必要,甚至可以说有点浪费资源,但是因为我看不惯原本的延时函数那么复杂的一串代码,所以我还是把那个代码简化了一下。

//delay.h
#ifndef DELAY_H

#define DELAY_H

void delay_ms(int ms);//延时函数(以毫秒为单位)

#endif
//delay.c
# include "delay.h"


#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "inc/hw_timer.h"
#include "driverlib/rom.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/rom_map.h"

void delay_ms(int ms)
{
    SysCtlDelay(SysCtlClockGet()/3000*ms);
}

然后后来发现延时函数也可以采用宏定义的方式进行简化,这样的话其对资源的消耗就几乎没有了。

#define delay_ms(x) SysCtlDelay(SysCtlClockGet()/3000*x)

3.GPIO中断

这一块是GPIO输入中断的库函数,中断一直都是单片机里面非常实用的一个功能,这一块配置了A7端口的上升沿中断,并通过中断处理计算间隔时间。

//gpio_int.h
# ifndef GPIO_INT_H

# define GPIO_INT_H

void GPIO_Int_Init();//初始化I/O中断
void IntHandlerGPIOA();//I/O口中断处理函数


#endif
//gpio_int.c
#include "gpio_int.h"


#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "inc/hw_timer.h"
#include "driverlib/rom.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/rom_map.h"

bool bFinishFlag = false;
unsigned int gpio_Frequency=0;

void GPIO_Int_Init()
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOA));
    GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_7);
    GPIOIntEnable(GPIO_PORTA_BASE, GPIO_PIN_7);
    GPIOIntTypeSet(GPIO_PORTA_BASE, GPIO_PIN_7, GPIO_RISING_EDGE);
    GPIOIntRegister(GPIO_PORTA_BASE, IntHandlerGPIOA);
    IntEnable(INT_GPIOA);
    IntMasterEnable();
}

void IntHandlerGPIOA()
{
    uint32_t ui32IntStatus;
    static uint32_t ui32Time[2] = {0};
    static uint8_t ui8EdgeCount = 0;
    double fPeriod = 0;
    ui32IntStatus = GPIOIntStatus(GPIO_PORTA_BASE, true);
    GPIOIntClear(GPIO_PORTA_BASE, ui32IntStatus);
    if((ui32IntStatus & GPIO_PIN_7) == GPIO_PIN_7)
    {
        ui32Time[ui8EdgeCount++] = TimerValueGet(TIMER0_BASE, TIMER_A);
        if(ui8EdgeCount > 1) {
            ui8EdgeCount = 0;
            fPeriod = ui32Time[1] > ui32Time[0] ? ui32Time[1] - ui32Time[0]: ui32Time[1] - ui32Time[0] + 0xFFFFFFFF;
            gpio_Frequency = 80000000.0/fPeriod;
            bFinishFlag = true;
        }
    }
}

4.按键

这里的库函数使用的按键是开发板上自带的两个按键,并且写了两种配置方式:普通输入和按键中断。其实本质上说按键中断也算是GPIO中断。这里通过中断实现了获取按下按键的键值的功能。

//key.h
#ifndef KEY_H

#define KEY_H

void key_Init();//按键1和2初始化
void key_Int_Init();//按键1中断初始化
void IntHandler_GPIOF();//按键1中断处理函数(需要在key.c中修改功能)

#define SW_1 (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4)&GPIO_PIN_4)?0:1//检测按键1是否按下,1为按下,0为没有按下
#define SW_2 (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_0)&GPIO_PIN_0)?0:1//检测按键2是否按下,1为按下,0为没有按下


#endif
//key.c
#include "key.h"


#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "inc/hw_gpio.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom_map.h"
#include "driverlib\sysctl.h"
#include "libraries\delay\delay.h"
#include "libraries\LED\led.h"
#include "libraries\KEY\key.h"
#include "driverlib/interrupt.h"
#include "inc/hw_types.h"

int key_flag=0;

void key_Init()
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF));
    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);
    GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
    HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
    HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x1;
    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0);
    GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
}

void key_Int_Init()
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF));
    HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
    HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x1;
    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4 | GPIO_PIN_0);
    GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4 | GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
    GPIOIntEnable(GPIO_PORTF_BASE, GPIO_PIN_4 | GPIO_PIN_0);
    GPIOIntTypeSet(GPIO_PORTF_BASE, GPIO_PIN_4 | GPIO_PIN_0, GPIO_FALLING_EDGE);
    IntPrioritySet (INT_GPIOF, 0xE1);
    GPIOIntRegister(GPIO_PORTF_BASE, IntHandler_GPIOF);
    IntEnable(INT_GPIOF);
    IntMasterEnable();
}

void IntHandler_GPIOF()
{
    GPIOIntClear(GPIO_PORTF_BASE, GPIOIntStatus(GPIO_PORTF_BASE, true));
    if(SW_1)key_flag|=1;
    if(SW_2)key_flag|=2;
}

5.LED

我这块开发板上的三色LED也可以说是一个不错的功能了,通过三色组合可以创造出很多不同的颜色,这里的库函数提供了普通的点灯功能以及通过pwm初始化形成不同颜色的灯光的功能,并且写了一个RGB函数来快速改变颜色。同时我在宏定义中抄了一张标准的RGB色表,预定义了很多颜色(当然因为是发光所以颜色并不准),可以尝试一下使用。

//led.h #ifndef LED_H #define LED_H #define MAX_DUTY 10000 void LED_Init();//三色LED灯初始化 void RGB_Init();//RGB输出模式初始化 void RGB(double red,double green,double blue);//设置输出颜色 #define RED_1 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1)//红灯亮 #define BLUE_1 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2)//蓝灯亮 #define GREEN_1 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, GPIO_PIN_3)//绿灯亮 #define RED_0 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0)//红灯灭 #define BLUE_0 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0)//蓝灯灭 #define GREEN_0 GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0)//绿灯灭 #define RED_Status ((GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1)&GPIO_PIN_1) == GPIO_PIN_1)?1:0//获取红灯是否开启 #define BLUE_Status ((GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2)&GPIO_PIN_2) == GPIO_PIN_2)?1:0//获取蓝灯是否开启 #define GREEN_Status ((GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_3)&GPIO_PIN_3) == GPIO_PIN_3)?1:0//获取绿灯是否开启 //预定义颜色 //参考网址:http://www.wahart.com.hk/rgb.htm #define Snow RGB(255,250,250) #define GhostWhite RGB(248,248,255) #define WhiteSmoke RGB(245,245,245) #define Gainsboro RGB(220,220,220) #define FloralWhite RGB(255,250,240) #define OldLace RGB(253,245,230) #define Linen RGB(250,240,230) #define AntiqueWhite RGB(250,235,215) #define PapayaWhip RGB(255,239,213) #define BlanchedAlmond RGB(255,235,205) #define Bisque RGB(255,228,196) #define PeachPuff RGB(255,218,185) #define NavajoWhite RGB(255,222,173) #define Moccasin RGB(255,228,181) #define Cornsilk RGB(255,248,220) #define Ivory RGB(255,255,240) #define LemonChiffon RGB(255,250,205) #define Seashell RGB(255,245,238) #define Honeydew RGB(240,255,240) #define MintCream RGB(245,255,250) #define Azure RGB(240,255,255) #define AliceBlue RGB(240,248,255) #define lavender RGB(230,230,250) #define LavenderBlush RGB(255,240,245) #define MistyRose RGB(255,228,225) #define White RGB(255,255,255) #define Black RGB( 0, 0, 0) #define DarkSlateGray RGB( 47, 79, 79) #define DimGrey RGB(105,105,105) #define SlateGrey RGB(112,128,144) #define LightSlateGray RGB(119,136,153) #define Grey RGB(190,190,190) #define LightGray RGB(211,211,211) #define MidnightBlue RGB( 25, 25,112) #define NavyBlue RGB( 0, 0,128) #define CornflowerBlue RGB(100,149,237) #define DarkSlateBlue RGB( 72, 61,139) #define SlateBlue RGB(106, 90,205) #define MediumSlateBlue RGB(123,104,238) #define LightSlateBlue RGB(132,112,255) #define MediumBlue RGB( 0, 0,205) #define RoyalBlue RGB( 65,105,225) #define Blue RGB( 0, 0,255) #define DodgerBlue RGB( 30,144,255) #define DeepSkyBlue RGB( 0,191,255) #define SkyBlue RGB(135,206,235) #define LightSkyBlue RGB(135,206,250) #define SteelBlue RGB( 70,130,180) #define LightSteelBlue RGB(176,196,222) #define LightBlue RGB(173,216,230) #define PowderBlue RGB(176,224,230) #define PaleTurquoise RGB(175,238,238) #define DarkTurquoise RGB( 0,206,209) #define MediumTurquoise RGB( 72,209,204) #define Turquoise RGB( 64,224,208) #define Cyan RGB( 0,255,255) #define LightCyan RGB(224,255,255) #define CadetBlue RGB( 95,158,160) #define MediumAquamarine RGB(102,205,170) #define Aquamarine RGB(127,255,212) #define DarkGreen RGB( 0,100, 0) #define DarkOliveGreen RGB( 85,107, 47) #define DarkSeaGreen RGB(143,188,143) #define SeaGreen RGB( 46,139, 87) #define MediumSeaGreen RGB( 60,179,113) #define LightSeaGreen RGB( 32,178,170) #define PaleGreen RGB(152,251,152) #define SpringGreen RGB( 0,255,127) #define LawnGreen RGB(124,252, 0) #define Green RGB( 0,255, 0) #define Chartreuse RGB(127,255, 0) #define MedSpringGreen RGB( 0,250,154) #define GreenYellow RGB(173,255, 47) #define LimeGreen RGB( 50,205, 50) #define YellowGreen RGB(154,205, 50) #define ForestGreen RGB( 34,139, 34) #define OliveDrab RGB(107,142, 35) #define DarkKhaki RGB(189,183,107) #define PaleGoldenrod RGB(238,232,170) #define LtGoldenrodYello RGB(250,250,210) #define LightYellow RGB(255,255,224) #define Yellow RGB(255,255, 0) #define Gold RGB(255,215, 0) #define LightGoldenrod RGB(238,221,130) #define goldenrod RGB(218,165, 32) #define DarkGoldenrod RGB(184,134, 11) #define RosyBrown RGB(188,143,143) #define IndianRed RGB(205, 92, 92) #define SaddleBrown RGB(139, 69, 19) #define Sienna RGB(160, 82, 45) #define Peru RGB(205,133, 63) #define Burlywood RGB(222,184,135) #define Beige RGB(245,245,220) #define Wheat RGB(245,222,179) #define SandyBrown RGB(244,164, 96) #define Tan RGB(210,180,140) #define Chocolate RGB(210,105, 30) #define Firebrick RGB(178, 34, 34) #define Brown RGB(165, 42, 42) #define DarkSalmon RGB(233,150,122) #define Salmon RGB(250,128,114) #define LightSalmon RGB(255,160,122) #define Orange RGB(255,165, 0) #define DarkOrange RGB(255,140, 0) #define Coral RGB(255,127, 80) #define LightCoral RGB(240,128,128) #define Tomato RGB(255, 99, 71) #define OrangeRed RGB(255, 69, 0) #define Red RGB(255, 0, 0) #define HotPink RGB(255,105,180) #define DeepPink RGB(255, 20,147) #define Pink RGB(255,192,203) #define LightPink RGB(255,182,193) #define PaleVioletRed RGB(219,112,147) #define Maroon RGB(176, 48, 96) #define MediumVioletRed RGB(199, 21,133) #define VioletRed RGB(208, 32,144) #define Magenta RGB(255, 0,255) #define Violet RGB(238,130,238) #define Plum RGB(221,160,221) #define Orchid RGB(218,112,214) #define MediumOrchid RGB(186, 85,211) #define DarkOrchid RGB(153, 50,204) #define DarkViolet RGB(148, 0,211) #define BlueViolet RGB(138, 43,226) #define Purple RGB(160, 32,240) #define MediumPurple RGB(147,112,219) #define Thistle RGB(216,191,216) #define Snow1 RGB(255,250,250) #define Snow2 RGB(238,233,233) #define Snow3 RGB(205,201,201) #define Snow4 RGB(139,137,137) #define Seashell1 RGB(255,245,238) #define Seashell2 RGB(238,229,222) #define Seashell3 RGB(205,197,191) #define Seashell4 RGB(139,134,130) #define AntiqueWhite1 RGB(255,239,219) #define AntiqueWhite2 RGB(238,223,204) #define AntiqueWhite3 RGB(205,192,176) #define AntiqueWhite4 RGB(139,131,120) #define Bisque1 RGB(255,228,196) #define Bisque2 RGB(238,213,183) #define Bisque3 RGB(205,183,158) #define Bisque4 RGB(139,125,107) #define PeachPuff1 RGB(255,218,185) #define PeachPuff2 RGB(238,203,173) #define PeachPuff3 RGB(205,175,149) #define PeachPuff4 RGB(139,119,101) #define NavajoWhite1 RGB(255,222,173) #define NavajoWhite2 RGB(238,207,161) #define NavajoWhite3 RGB(205,179,139) #define NavajoWhite4 RGB(139,121, 94) #define LemonChiffon1 RGB(255,250,205) #define LemonChiffon2 RGB(238,233,191) #define LemonChiffon3 RGB(205,201,165) #define LemonChiffon4 RGB(139,137,112) #define Cornsilk1 RGB(255,248,220) #define Cornsilk2 RGB(238,232,205) #define Cornsilk3 RGB(205,200,177) #define Cornsilk4 RGB(139,136,120) #define Ivory1 RGB(255,255,240) #define Ivory2 RGB(238,238,224) #define Ivory3 RGB(205,205,193) #define Ivory4 RGB(139,139,131) #define Honeydew1 RGB(240,255,240) #define Honeydew2 RGB(224,238,224) #define Honeydew3 RGB(193,205,193) #define Honeydew4 RGB(131,139,131) #define LavenderBlush1 RGB(255,240,245) #define LavenderBlush2 RGB(238,224,229) #define LavenderBlush3 RGB(205,193,197) #define LavenderBlush4 RGB(139,131,134) #define MistyRose1 RGB(255,228,225) #define MistyRose2 RGB(238,213,210) #define MistyRose3 RGB(205,183,181) #define MistyRose4 RGB(139,125,123) #define Azure1 RGB(240,255,255) #define Azure2 RGB(224,238,238) #define Azure3 RGB(193,205,205) #define Azure4 RGB(131,139,139) #define SlateBlue1 RGB(131,111,255) #define SlateBlue2 RGB(122,103,238) #define SlateBlue3 RGB(105, 89,205) #define SlateBlue4 RGB( 71, 60,139) #define RoyalBlue1 RGB( 72,118,255) #define RoyalBlue2 RGB( 67,110,238) #define RoyalBlue3 RGB( 58, 95,205) #define RoyalBlue4 RGB( 39, 64,139) #define Blue1 RGB( 0, 0,255) #define Blue2 RGB( 0, 0,238) #define Blue3 RGB( 0, 0,205) #define Blue4 RGB( 0, 0,139) #define DodgerBlue1 RGB( 30,144,255) #define DodgerBlue2 RGB( 28,134,238) #define DodgerBlue3 RGB( 24,116,205) #define DodgerBlue4 RGB( 16, 78,139) #define SteelBlue1 RGB( 99,184,255) #define SteelBlue2 RGB( 92,172,238) #define SteelBlue3 RGB( 79,148,205) #define SteelBlue4 RGB( 54,100,139) #define DeepSkyBlue1 RGB( 0,191,255) #define DeepSkyBlue2 RGB( 0,178,238) #define DeepSkyBlue3 RGB( 0,154,205) #define DeepSkyBlue4 RGB( 0,104,139) #define SkyBlue1 RGB(135,206,255) #define SkyBlue2 RGB(126,192,238) #define SkyBlue3 RGB(108,166,205) #define SkyBlue4 RGB( 74,112,139) #define LightSkyBlue1 RGB(176,226,255) #define LightSkyBlue2 RGB(164,211,238) #define LightSkyBlue3 RGB(141,182,205) #define LightSkyBlue4 RGB( 96,123,139) #define SlateGray1 RGB(198,226,255) #define SlateGray2 RGB(185,211,238) #define SlateGray3 RGB(159,182,205) #define SlateGray4 RGB(108,123,139) #define LightSteelBlue1 RGB(202,225,255) #define LightSteelBlue2 RGB(188,210,238) #define LightSteelBlue3 RGB(162,181,205) #define LightSteelBlue4 RGB(110,123,139) #define LightBlue1 RGB(191,239,255) #define LightBlue2 RGB(178,223,238) #define LightBlue3 RGB(154,192,205) #define LightBlue4 RGB(104,131,139) #define LightCyan1 RGB(224,255,255) #define LightCyan2 RGB(209,238,238) #define LightCyan3 RGB(180,205,205) #define LightCyan4 RGB(122,139,139) #define PaleTurquoise1 RGB(187,255,255) #define PaleTurquoise2 RGB(174,238,238) #define PaleTurquoise3 RGB(150,205,205) #define PaleTurquoise4 RGB(102,139,139) #define CadetBlue1 RGB(152,245,255) #define CadetBlue2 RGB(142,229,238) #define CadetBlue3 RGB(122,197,205) #define CadetBlue4 RGB( 83,134,139) #define Turquoise1 RGB( 0,245,255) #define Turquoise2 RGB( 0,229,238) #define Turquoise3 RGB( 0,197,205) #define Turquoise4 RGB( 0,134,139) #define Cyan1 RGB( 0,255,255) #define Cyan2 RGB( 0,238,238) #define Cyan3 RGB( 0,205,205) #define Cyan4 RGB( 0,139,139) #define DarkSlateGray1 RGB(151,255,255) #define DarkSlateGray2 RGB(141,238,238) #define DarkSlateGray3 RGB(121,205,205) #define DarkSlateGray4 RGB( 82,139,139) #define Aquamarine1 RGB(127,255,212) #define Aquamarine2 RGB(118,238,198) #define Aquamarine3 RGB(102,205,170) #define Aquamarine4 RGB( 69,139,116) #define DarkSeaGreen1 RGB(193,255,193) #define DarkSeaGreen2 RGB(180,238,180) #define DarkSeaGreen3 RGB(155,205,155) #define DarkSeaGreen4 RGB(105,139,105) #define SeaGreen1 RGB( 84,255,159) #define SeaGreen2 RGB( 78,238,148) #define SeaGreen3 RGB( 67,205,128) #define SeaGreen4 RGB( 46,139, 87) #define PaleGreen1 RGB(154,255,154) #define PaleGreen2 RGB(144,238,144) #define PaleGreen3 RGB(124,205,124) #define PaleGreen4 RGB( 84,139, 84) #define SpringGreen1 RGB( 0,255,127) #define SpringGreen2 RGB( 0,238,118) #define SpringGreen3 RGB( 0,205,102) #define SpringGreen4 RGB( 0,139, 69) #define Green1 RGB( 0,255, 0) #define Green2 RGB( 0,238, 0) #define Green3 RGB( 0,205, 0) #define Green4 RGB( 0,139, 0) #define Chartreuse1 RGB(127,255, 0) #define Chartreuse2 RGB(118,238, 0) #define Chartreuse3 RGB(102,205, 0) #define Chartreuse4 RGB( 69,139, 0) #define OliveDrab1 RGB(192,255, 62) #define OliveDrab2 RGB(179,238, 58) #define OliveDrab3 RGB(154,205, 50) #define OliveDrab4 RGB(105,139, 34) #define DarkOliveGreen1 RGB(202,255,112) #define DarkOliveGreen2 RGB(188,238,104) #define DarkOliveGreen3 RGB(162,205, 90) #define DarkOliveGreen4 RGB(110,139, 61) #define Khaki1 RGB(255,246,143) #define Khaki2 RGB(238,230,133) #define Khaki3 RGB(205,198,115) #define Khaki4 RGB(139,134, 78) #define LightGoldenrod1 RGB(255,236,139) #define LightGoldenrod2 RGB(238,220,130) #define LightGoldenrod3 RGB(205,190,112) #define LightGoldenrod4 RGB(139,129, 76) #define LightYellow1 RGB(255,255,224) #define LightYellow2 RGB(238,238,209) #define LightYellow3 RGB(205,205,180) #define LightYellow4 RGB(139,139,122) #define Yellow1 RGB(255,255, 0) #define Yellow2 RGB(238,238, 0) #define Yellow3 RGB(205,205, 0) #define Yellow4 RGB(139,139, 0) #define Gold1 RGB(255,215, 0) #define Gold2 RGB(238,201, 0) #define Gold3 RGB(205,173, 0) #define Gold4 RGB(139,117, 0) #define Goldenrod1 RGB(255,193, 37) #define Goldenrod2 RGB(238,180, 34) #define Goldenrod3 RGB(205,155, 29) #define Goldenrod4 RGB(139,105, 20) #define DarkGoldenrod1 RGB(255,185, 15) #define DarkGoldenrod2 RGB(238,173, 14) #define DarkGoldenrod3 RGB(205,149, 12) #define DarkGoldenrod4 RGB(139,101, 8) #define RosyBrown1 RGB(255,193,193) #define RosyBrown2 RGB(238,180,180) #define RosyBrown3 RGB(205,155,155) #define RosyBrown4 RGB(139,105,105) #define IndianRed1 RGB(255,106,106) #define IndianRed2 RGB(238, 99, 99) #define IndianRed3 RGB(205, 85, 85) #define IndianRed4 RGB(139, 58, 58) #define Sienna1 RGB(255,130, 71) #define Sienna2 RGB(238,121, 66) #define Sienna3 RGB(205,104, 57) #define Sienna4 RGB(139, 71, 38) #define Burlywood1 RGB(255,211,155) #define Burlywood2 RGB(238,197,145) #define Burlywood3 RGB(205,170,125) #define Burlywood4 RGB(139,115, 85) #define Wheat1 RGB(255,231,186) #define Wheat2 RGB(238,216,174) #define Wheat3 RGB(205,186,150) #define Wheat4 RGB(139,126,102) #define Tan1 RGB(255,165, 79) #define Tan2 RGB(238,154, 73) #define Tan3 RGB(205,133, 63) #define Tan4 RGB(139, 90, 43) #define Chocolate1 RGB(255,127, 36) #define Chocolate2 RGB(238,118, 33) #define Chocolate3 RGB(205,102, 29) #define Chocolate4 RGB(139, 69, 19) #define Firebrick1 RGB(255, 48, 48) #define Firebrick2 RGB(238, 44, 44) #define Firebrick3 RGB(205, 38, 38) #define Firebrick4 RGB(139, 26, 26) #define Brown1 RGB(255, 64, 64) #define Brown2 RGB(238, 59, 59) #define Brown3 RGB(205, 51, 51) #define Brown4 RGB(139, 35, 35) #define Salmon1 RGB(255,140,105) #define Salmon2 RGB(238,130, 98) #define Salmon3 RGB(205,112, 84) #define Salmon4 RGB(139, 76, 57) #define LightSalmon1 RGB(255,160,122) #define LightSalmon2 RGB(238,149,114) #define LightSalmon3 RGB(205,129, 98) #define LightSalmon4 RGB(139, 87, 66) #define Orange1 RGB(255,165, 0) #define Orange2 RGB(238,154, 0) #define Orange3 RGB(205,133, 0) #define Orange4 RGB(139, 90, 0) #define DarkOrange1 RGB(255,127, 0) #define DarkOrange2 RGB(238,118, 0) #define DarkOrange3 RGB(205,102, 0) #define DarkOrange4 RGB(139, 69, 0) #define Coral1 RGB(255,114, 86) #define Coral2 RGB(238,106, 80) #define Coral3 RGB(205, 91, 69) #define Coral4 RGB(139, 62, 47) #define Tomato1 RGB(255, 99, 71) #define Tomato2 RGB(238, 92, 66) #define Tomato3 RGB(205, 79, 57) #define Tomato4 RGB(139, 54, 38) #define OrangeRed1 RGB(255, 69, 0) #define OrangeRed2 RGB(238, 64, 0) #define OrangeRed3 RGB(205, 55, 0) #define OrangeRed4 RGB(139, 37, 0) #define Red1 RGB(255, 0, 0) #define Red2 RGB(238, 0, 0) #define Red3 RGB(205, 0, 0) #define Red4 RGB(139, 0, 0) #define DeepPink1 RGB(255, 20,147) #define DeepPink2 RGB(238, 18,137) #define DeepPink3 RGB(205, 16,118) #define DeepPink4 RGB(139, 10, 80) #define HotPink1 RGB(255,110,180) #define HotPink2 RGB(238,106,167) #define HotPink3 RGB(205, 96,144) #define HotPink4 RGB(139, 58, 98) #define Pink1 RGB(255,181,197) #define Pink2 RGB(238,169,184) #define Pink3 RGB(205,145,158) #define Pink4 RGB(139, 99,108) #define LightPink1 RGB(255,174,185) #define LightPink2 RGB(238,162,173) #define LightPink3 RGB(205,140,149) #define LightPink4 RGB(139, 95,101) #define PaleVioletRed1 RGB(255,130,171) #define PaleVioletRed2 RGB(238,121,159) #define PaleVioletRed3 RGB(205,104,137) #define PaleVioletRed4 RGB(139, 71, 93) #define Maroon1 RGB(255, 52,179) #define Maroon2 RGB(238, 48,167) #define Maroon3 RGB(205, 41,144) #define Maroon4 RGB(139, 28, 98) #define VioletRed1 RGB(255, 62,150) #define VioletRed2 RGB(238, 58,140) #define VioletRed3 RGB(205, 50,120) #define VioletRed4 RGB(139, 34, 82) #define Magenta1 RGB(255, 0,255) #define Magenta2 RGB(238, 0,238) #define Magenta3 RGB(205, 0,205) #define Magenta4 RGB(139, 0,139) #define Orchid1 RGB(255,131,250) #define Orchid2 RGB(238,122,233) #define Orchid3 RGB(205,105,201) #define Orchid4 RGB(139, 71,137) #define Plum1 RGB(255,187,255) #define Plum2 RGB(238,174,238) #define Plum3 RGB(205,150,205) #define Plum4 RGB(139,102,139) #define MediumOrchid1 RGB(224,102,255) #define MediumOrchid2 RGB(209, 95,238) #define MediumOrchid3 RGB(180, 82,205) #define MediumOrchid4 RGB(122, 55,139) #define DarkOrchid1 RGB(191, 62,255) #define DarkOrchid2 RGB(178, 58,238) #define DarkOrchid3 RGB(154, 50,205) #define DarkOrchid4 RGB(104, 34,139) #define Purple1 RGB(155, 48,255) #define Purple2 RGB(145, 44,238) #define Purple3 RGB(125, 38,205) #define Purple4 RGB( 85, 26,139) #define MediumPurple1 RGB(171,130,255) #define MediumPurple2 RGB(159,121,238) #define MediumPurple3 RGB(137,104,205) #define MediumPurple4 RGB( 93, 71,139) #define Thistle1 RGB(255,225,255) #define Thistle2 RGB(238,210,238) #define Thistle3 RGB(205,181,205) #define Thistle4 RGB(139,123,139) #define grey11 RGB( 28, 28, 28) #define grey21 RGB( 54, 54, 54) #define grey31 RGB( 79, 79, 79) #define grey41 RGB(105,105,105) #define grey51 RGB(130,130,130) #define grey61 RGB(156,156,156) #define grey71 RGB(181,181,181) #define gray81 RGB(207,207,207) #define gray91 RGB(232,232,232) #define DarkGrey RGB(169,169,169) #define DarkBlue RGB( 0, 0,139) #define DarkCyan RGB( 0,139,139) #define DarkMagenta RGB(139, 0,139) #define DarkRed RGB(139, 0, 0) #define LightGreen RGB(144,238,144) #endif
//led.c #include "led.h" #include <stdint.h> #include <stdbool.h> #include "inc/hw_types.h" #include "inc/hw_memmap.h" #include "inc/hw_gpio.h" #include "driverlib/sysctl.h" #include "driverlib/pin_map.h" #include "driverlib/rom_map.h" #include "driverlib/gpio.h" #include "driverlib/pwm.h" void LED_Init() { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF)); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 |GPIO_PIN_3); RED_0; BLUE_0; GREEN_0; } void RGB_Init() { MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 |GPIO_PIN_3); RED_0; BLUE_0; GREEN_0; MAP_GPIOPinConfigure(GPIO_PF1_M1PWM5); MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1); MAP_GPIOPinConfigure(GPIO_PF2_M1PWM6); MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2); MAP_GPIOPinConfigure(GPIO_PF3_M1PWM7); MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_3); SysCtlPWMClockSet(SYSCTL_PWMDIV_2); PWMGenConfigure(PWM1_BASE, PWM_GEN_2,PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenConfigure(PWM1_BASE, PWM_GEN_3,PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2,MAX_DUTY ); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3,MAX_DUTY ); PWMOutputState(PWM1_BASE,PWM_OUT_5_BIT, true); PWMOutputState(PWM1_BASE,PWM_OUT_6_BIT, true); PWMOutputState(PWM1_BASE,PWM_OUT_7_BIT, true); PWMGenEnable(PWM1_BASE, PWM_GEN_2); PWMGenEnable(PWM1_BASE, PWM_GEN_3); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5,MAX_DUTY); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6,MAX_DUTY); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,MAX_DUTY); } void RGB(double red,double green,double blue) { red=red/256.0*MAX_DUTY+1; green=green/256.0*MAX_DUTY+1; blue=blue/256.0*MAX_DUTY+1; if(red<0)red=1; if(green<0)green=1; if(blue<0)blue=1; PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5,(int)red); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6,(int)blue); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,(int)green); }

6.电机

用单片机来操控电机可以说是自控方面最常遇到的情况了,当然其实现也比较简单。通过连接电机驱动模块就可以轻松使用I/O口来操控电机。这里的库函数实现了对单个单机的操控,当然可以再加上对多个电机的操控。
注:不同的电机模块的操作方式不尽相同,不过我也忘了我的模块型号是啥了……

//motor.h #ifndef MOTOR_H #define MOTOR_H void Motor_Init(); void Motor_Adjust(int Value); #endif
//motor.c #include "motor.h" #include <stdint.h> #include <stdbool.h> #include "inc/hw_memmap.h" #include "driverlib/pin_map.h" #include "inc/hw_types.h" #include "driverlib/gpio.h" #include "driverlib/pwm.h" #include "driverlib/sysctl.h" void Motor_Init() { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); SysCtlPWMClockSet(SYSCTL_PWMDIV_4); SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); GPIOPinConfigure(GPIO_PB6_M0PWM0); GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6); PWMGenConfigure(PWM0_BASE, PWM_GEN_0,PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC); PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 1000); PWMGenEnable(PWM0_BASE, PWM_GEN_0); PWMOutputState(PWM0_BASE,PWM_OUT_0_BIT, true); PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0,1); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5); GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5, 0x0); } void Motor_Adjust(int Value) { if(Value<-100)Value=-100; if(Value>100)Value=100; if(Value<0) { Value=-Value; Value=(Value+100)/2; PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, Value*10); GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5, ~GPIO_PIN_4|GPIO_PIN_5); } else if(Value>0) { Value=(Value+100)/2; PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, Value*10); GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_4|~GPIO_PIN_5); } else if(Value==0) { Value=1; PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, Value*10); } }

7.OLED

使用单片机通过屏幕输出信息也是非常常见的需求,这里贴的代码改编自我的一位学长,这代码调用起来也是相当方便的。

//oled.h /* * oled.h * * Created on: 2016年10月17日 * Author: weycen */ #ifndef OLED_H_ #define OLED_H_ #include <stdint.h> /* Peripheral definitions for EK-TM4C123G board */ // I2C port #define OLED_I2C_BASE I2C0_BASE #define OLED_I2C_SYSCTL_PERIPH SYSCTL_PERIPH_I2C0 // GPIO for I2C pins #define OLED_GPIO_PORT_BASE GPIO_PORTB_BASE #define OLED_GPIO_SYSCTL_PERIPH SYSCTL_PERIPH_GPIOB #define OLED_I2C_SCL GPIO_PIN_2 #define OLED_I2C_SDA GPIO_PIN_3 #define OLED_I2C_PINS (OLED_I2C_SCL | OLED_I2C_SDA) // GPIO pin configuer #define OLED_GPIO_I2C_SCL GPIO_PB2_I2C0SCL #define OLED_GPIO_I2C_SDA GPIO_PB3_I2C0SDA // commands define #define OLED_I2C_ADDR 0x3C #define OLED_I2C_DAT 0x40 #define OLED_I2C_CMD 0x00 #define WRITE false #define READ true #define OLED_CMD 1 #define OLED_DAT 0 #define OLED_DELAY 600 extern void DelayXms(uint16_t xms); extern void OLED_I2CInit(void); extern void OLED_DrawPiexl(uint8_t u8X,uint8_t u8Y,bool bFILL); extern void OLED_FillScreen(void); extern void LCDDrawLine(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,bool FILL); extern void OLED_ClearScreen(); extern void OLED_DisplayChinese(uint8_t u8X,uint8_t u8Y,uint8_t number); extern void OLED_DisplayChar(uint8_t u8X,uint8_t u8Y,uint8_t chr); extern void OLED_DisplayNumber(uint8_t u8X,uint8_t u8Y,double num,uint8_t dec_dig); #endif /* OLED_H_ */
//oled.c /* * oled.c * * Created on: 2016年10月17日 * Author: weycen */ ////OLED的显存 ////存放格式如下. ////[0]0 1 2 3 ... 127 // . // . // . // . // . // . // . // . ////[1]0 1 2 3 ... 127 ////[2]0 1 2 3 ... 127 ////[3]0 1 2 3 ... 127 ////[4]0 1 2 3 ... 127 ////[5]0 1 2 3 ... 127 ////[6]0 1 2 3 ... 127 ////[7]0 1 2 3 ... 127 #include <stdint.h> #include <stdio.h> #include <math.h> #include <stdbool.h> #include <string.h> #include "inc/hw_memmap.h" #include "inc/hw_i2c.h" #include "inc/hw_sysctl.h" #include "driverlib/sysctl.h" #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/i2c.h" #include "oled.h" #include "oled_font.h" #include "libraries/delay/delay.h" /***********************************/ void OLED_Configuer(void); void OLED_SetPosition(uint8_t u8X , uint8_t u8Y); void OLED_I2CSendByte(uint8_t CMD_or_DAT, uint8_t Byte); void OLED_FillScreen(void); /************************************/ /*************************************************** * 延时 s函数 * ***********************************************/ void DelayXs(uint16_t xs) { SysCtlDelay(xs*(SysCtlClockGet() / 3)); } /*************************************************** * 延时 ms函数 * ***********************************************/ void DelayXms(uint16_t xms) { SysCtlDelay(xms*(SysCtlClockGet() / 3000)); } /*************************************************** * 延时 us函数 * ***********************************************/ void DelayXus(uint16_t xus) { SysCtlDelay(xus*(SysCtlClockGet() / 3000000)); } //**************************************** // // init oled I2C // //**************************************** void OLED_I2CInit(void) { // SysCtlPeripheralEnable(OLED_GPIO_SYSCTL_PERIPH); SysCtlPeripheralEnable(OLED_I2C_SYSCTL_PERIPH); // GPIOPinTypeI2CSCL(OLED_GPIO_PORT_BASE, OLED_I2C_SCL); GPIOPinTypeI2C(OLED_GPIO_PORT_BASE, OLED_I2C_SDA); // GPIOPinConfigure(GPIO_PB2_I2C0SCL); GPIOPinConfigure(GPIO_PB3_I2C0SDA); // I2CMasterInitExpClk(OLED_I2C_BASE, SysCtlClockGet(), true); // OLED_Configuer(); delay_ms(200); OLED_ClearScreen(); } // // oled初始设置 // void OLED_Configuer(void) { /* display off */ OLED_I2CSendByte(OLED_CMD,0xAE); /* set lower column address */ OLED_I2CSendByte(OLED_CMD,0x00); /* set higher column address */ OLED_I2CSendByte(OLED_CMD,0x10); /* set display start line */ OLED_I2CSendByte(OLED_CMD,0x40); /* set page address */ OLED_I2CSendByte(OLED_CMD,0xB0); /* contract control */ OLED_I2CSendByte(OLED_CMD,0x81); OLED_I2CSendByte(OLED_CMD,0x66); /* set segment remap */ OLED_I2CSendByte(OLED_CMD,0xA1); /* normal / reverse */ OLED_I2CSendByte(OLED_CMD,0xA6); /* multiplex ratio */ OLED_I2CSendByte(OLED_CMD,0xA8); /* duty = 1/64 */ OLED_I2CSendByte(OLED_CMD,0x3F); /* Com scan direction */ OLED_I2CSendByte(OLED_CMD,0xC8);/* c3*/ /* set display offset */ OLED_I2CSendByte(OLED_CMD,0xD3); OLED_I2CSendByte(OLED_CMD,0x00); /* set osc division */ OLED_I2CSendByte(OLED_CMD,0xD5); OLED_I2CSendByte(OLED_CMD,0x80); /* set area color mode off */ OLED_I2CSendByte(OLED_CMD,0xD8); OLED_I2CSendByte(OLED_CMD,0x05); /* set pre-charge period */ OLED_I2CSendByte(OLED_CMD,0xD9); OLED_I2CSendByte(OLED_CMD,0xF1); /* set COM pins */ OLED_I2CSendByte(OLED_CMD,0xDA); OLED_I2CSendByte(OLED_CMD,0x12); /* set vcomh */ OLED_I2CSendByte(OLED_CMD,0xDB); OLED_I2CSendByte(OLED_CMD,0x30); /* set charge pump disable */ OLED_I2CSendByte(OLED_CMD,0x8D); OLED_I2CSendByte(OLED_CMD,0x14); /* display on*/ OLED_I2CSendByte(OLED_CMD,0xAF); } // // I2C发送一个字节 // void OLED_I2CSendByte(uint8_t CMD_or_DAT,uint8_t Byte) { uint8_t i2cWriteBuffer; // frame 1: send slave_addr + R/W 0011 1100 I2CMasterSlaveAddrSet(OLED_I2C_BASE, OLED_I2C_ADDR, false); // frame 2: I2CMasterControl(OLED_I2C_BASE, I2C_MASTER_CMD_BURST_SEND_START); // i2cWriteBuffer = (CMD_or_DAT == OLED_CMD) ? OLED_I2C_CMD : OLED_I2C_DAT; // frame 3: I2CMasterDataPut(OLED_I2C_BASE, i2cWriteBuffer); I2CMasterControl(OLED_I2C_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); // wait for master free while(I2CMasterBusy(OLED_I2C_BASE)) ; // frame 4: i2cWriteBuffer = Byte; I2CMasterDataPut(OLED_I2C_BASE, i2cWriteBuffer); // wait for master free while(I2CMasterBusy(OLED_I2C_BASE)) ; I2CMasterControl(OLED_I2C_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH); // wait for master free while(I2CMasterBusy(OLED_I2C_BASE)) ; } // // 在指定位置打点函数 // bFILL: true 填充 ,false 清除 void OLED_DrawPiexl(uint8_t u8X,uint8_t u8Y,bool bFILL) { uint8_t temp; // 限幅 u8X = u8X > 127 ? 127 : u8X; u8Y = u8Y > 63 ? 63 : u8Y; if( bFILL ) temp = 0x01 << ( u8Y % 8 ); else temp = ~ ( 0x01 << ( u8Y % 8 ) ); // 设置目标点page OLED_SetPosition(u8X,u8Y); // 发送目标点数据 OLED_I2CSendByte(OLED_DAT,temp); } // // 设置坐标位置 x:0-127, y:0-63 // void OLED_SetPosition( uint8_t u8X , uint8_t u8Y ) { OLED_I2CSendByte( OLED_CMD , 0xB0 + u8Y / 8 ); // y page OLED_I2CSendByte( OLED_CMD , ( ( u8X & 0xF0 ) >> 4 ) | 0x10 ); // x轴 OLED_I2CSendByte( OLED_CMD , ( u8X & 0x0F ) ); } //void OLED_SetPosition( uint8_t u8X , uint8_t u8Y ) //{ // OLED_I2CSendByte( OLED_CMD , 0xB0 + u8Y); // y page // OLED_I2CSendByte( OLED_CMD , ( ( u8X & 0xF0 ) >> 4 ) | 0x10 ); // x轴 // OLED_I2CSendByte( OLED_CMD , ( u8X & 0x0F ) ); //} // // 打开显示 // void OLED_DisplayOn(void) { OLED_I2CSendByte(OLED_CMD,0X8D); //SET DCDC命令 OLED_I2CSendByte(OLED_CMD,0X14); //DCDC ON OLED_I2CSendByte(OLED_CMD,0XAF); //DISPLAY ON } // // 关闭显示 // void OLED_DisplayOff(void) { OLED_I2CSendByte(OLED_CMD,0X8D); //SET DCDC命令 OLED_I2CSendByte(OLED_CMD,0X10); //DCDC off OLED_I2CSendByte(OLED_CMD,0XAE); //DISPLAY off } // // 清屏函数 // void OLED_ClearScreen(void) { // uint8_t x,y; // for(x=0;x<128;x++) // 128列 // { // for(y=0;y<8;y++) // { // OLED_SetPosition(x,y*8); // 8页 // OLED_I2CSendByte(OLED_DAT,0x00); //每页竖着8位均写0 // } // } uint8_t i,n; for(i=0;i<8;i++) { OLED_I2CSendByte(OLED_CMD,0xB0 + i); OLED_I2CSendByte(OLED_CMD,0x00); OLED_I2CSendByte(OLED_CMD,0x10); for(n=0;n<128;n++) OLED_I2CSendByte(OLED_DAT,0x0); } //更新显示 } // // oled写满屏 // void OLED_FillScreen(void) { // uint8_t x,y; // for(x=0;x<128;x++) // 128列 // { // for(y=0;y<8;y++) // { // OLED_SetPosition(x,y*8); // 8页 // OLED_I2CSendByte(OLED_DAT,0xFF); //每页竖着8位均写0 // } // } uint8_t i,n; for(i=0;i<8;i++) { OLED_I2CSendByte(OLED_CMD,0xB0 + i); OLED_I2CSendByte(OLED_CMD,0x00); OLED_I2CSendByte(OLED_CMD,0x10); for(n=0;n<128;n++) OLED_I2CSendByte(OLED_DAT,0xAA); } //更新显示 } /************************************************************* 函数名: LCDDrawLine 函数说明:使用Bresenham法,画任意两点间的直线 传入参数:(x0,y0),竖直线的起点;(x1,y1)竖直线的终点 color=1,点亮;color=0,擦除 传出参数:无 返回值: 无 ************************************************************/ void LCDDrawLine(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,bool FILL) { int32_t dx; // 直线x轴差值变量 int32_t dy; // 直线y轴差值变量 int32_t dx_sym; // x轴增长方向,为-1时减值方向,为1时增值方向 int32_t dy_sym; // y轴增长方向,为-1时减值方向,为1时增值方向 int32_t dx_2; // dx*2值变量,用于加快运算速度 int32_t dy_2; // dy*2值变量,用于加快运算速度 int32_t di; // 决策变量 dx = x1-x0; // 求取两点之间的差值 dy = y1-y0; if(dx<0) dx_sym=-1; else{ if(dx>0) dx_sym=1; } if(dy>0) dy_sym=1; else{ if(dy<0) dy_sym=-1; } dx=dx_sym*dx; dy=dy_sym*dy; dx_2=dx*2; dy_2=dy*2; if(dx>=dy) { di=dy_2-dx; while(x0!=x1) { OLED_DrawPiexl(x0,y0,FILL); x0+=dx_sym; if(di<0) di+=dy_2; else{ di+=dy_2-dx_2; y0+=dy_sym; } } OLED_DrawPiexl(x0,y0,FILL); }else{ di=dx_2-dy; while(y0!=y1) { OLED_DrawPiexl(x0,y0,FILL); y0+=dy_sym; if(di<0) di+=dx_2; else{ di+=dx_2-dy_2; x0+=dx_sym; } } OLED_DrawPiexl(x0,y0,FILL); } } // //显示汉字 // void OLED_DisplayChinese(uint8_t u8X,uint8_t u8Y,uint8_t number) { uint8_t i/*adder=0*/; uint8_t temp; temp = 2 * number; OLED_SetPosition(u8X,u8Y); // 起始点,即页 for( i = 0; i < 16; i++ ) { OLED_I2CSendByte( OLED_DAT, FONT_16_16[ temp ][ i ] ); //OLED_WR_Byte(Hzk[2*no][t],OLED_DATA); //adder + = 1; } OLED_SetPosition( u8X, u8Y + 8 ); // 移到下一页 for( i = 0; i < 16; i++ ) { OLED_I2CSendByte( OLED_DAT, FONT_16_16[ temp + 1 ][ i ] ); //OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA); //adder+=1; } } //在指定位置显示一个字符,包括部分字符 //x:0~127 //y:0~63 //mode:0,反白显示;1,正常显示 //size:选择字体 16/12 void OLED_DisplayChar(uint8_t u8X,uint8_t u8Y,uint8_t chr) { uint8_t i=0 , c=0; uint16_t temp ; c = chr-' ';//得到偏移后的值 asic码值 temp = c*16; // if(Char_Size ==16) // { OLED_SetPosition(u8X,u8Y); for(i=0;i<8;i++) OLED_I2CSendByte(OLED_DAT,FONT_8_16[temp+i]); OLED_SetPosition(u8X,u8Y+8); for(i=0;i<8;i++) OLED_I2CSendByte(OLED_DAT,FONT_8_16[temp+i+8]); // } // else { // OLED_SetPosition(u8X,u8Y); // for(i=0;i<6;i++) // OLED_I2CSendByte(OLED_DAT,FONT_8_16[c][i]); // } } /************************************************** *名称:void disp_num(double num,uchar dec_dig) *功能:显示任意实数,并可设定保留小数位数,且有四舍五入功能 *入口参数:num :要显示的数 , dec_dig :保留小数位 **************************************************/ void OLED_DisplayNumber(uint8_t u8X,uint8_t u8Y,double num,uint8_t dec_dig) { //uint32_t a; int num_int,num_dec_int,m=0,k=0,j; double num_dec; uint8_t xtemp,ytemp; xtemp =u8X; ytemp =u8Y; uint8_t integ[ 10 ],decim[ 10 ]; num_int = ( int )( num + 5 * pow( 10,-( dec_dig + 1 ) ) );/* a= 1234 */ num_dec = num - num_int; /* b= 0.5678 */ for( j = num_int; j > 0; j = j / 10 ) { integ[k++]=j % 10; /* 整数位分解 */ } num_dec_int = ( int )( num_dec * pow( 10 , dec_dig ) ); /* 小数位变整数 */ for(j=num_dec_int;j>0;j=j/10) { decim[m++]=j%10; /* 小数位分解 */ } /* 显示整数位 */ for(j=k-1;j>=0;j--) { OLED_DisplayChar(xtemp,ytemp,integ[j]+48); //LCDSendData(DAT,integ[j]+48); xtemp+=8; } if(num_int==0) { OLED_DisplayChar(xtemp,ytemp,'0'); //LCDSendData(DAT,'0'); xtemp+=8; } /* 显示整数 小数之间小数点*/ OLED_DisplayChar(xtemp,ytemp,'.'); //LCDSendData(DAT,'.'); xtemp+=8; /* 显示小数位 */ for(j=dec_dig-1;j>=0;j--) { OLED_DisplayChar(xtemp,ytemp,decim[j]+48); //LCDSendData(DAT,decim[j]+48); xtemp+=8; } }

8.PWM

其实之前led的时候已经用过pwm了,不过这个库函数的目的是通过其他的I/O口输出PWM波,所以把它们分成了两个文件。

//pwm.h #ifndef PWM_H #define PWM_H #include <stdint.h> #include <stdbool.h> void PWM_Init(uint32_t ui32Frequency);//初始化PWM输出 void Adjust_PWM(int duty,int frequency);//调整PWM频率和占空比(参数为0表示不改变) #endif
//pwm.c #include "pwm.h" #include <stdint.h> #include <stdbool.h> #include "inc/hw_types.h" #include "inc/hw_memmap.h" #include "inc/hw_ints.h" #include "inc/hw_timer.h" #include "driverlib/rom.h" #include "driverlib/gpio.h" #include "driverlib/pwm.h" #include "driverlib/interrupt.h" #include "driverlib/pin_map.h" #include "driverlib/sysctl.h" #include "driverlib/timer.h" #include "driverlib/uart.h" #include "utils/uartstdio.h" #include "driverlib/rom_map.h" int _frequency=0; void PWM_Init(uint32_t ui32Frequency) { _frequency=ui32Frequency; SysCtlPWMClockSet(SYSCTL_PWMDIV_1); SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinConfigure(GPIO_PA6_M1PWM2); GPIOPinTypePWM(GPIO_PORTA_BASE, GPIO_PIN_6); PWMGenConfigure(PWM1_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN |PWM_GEN_MODE_NO_SYNC); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, SysCtlClockGet()/ui32Frequency); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_2, PWMGenPeriodGet(PWM1_BASE, PWM_GEN_1)/2); PWMOutputState(PWM1_BASE, PWM_OUT_2_BIT, true); PWMGenEnable(PWM1_BASE, PWM_GEN_1); } void Adjust_PWM(int duty,int frequency) { static int _duty=50; if(!duty) { PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, SysCtlClockGet()/_frequency); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_2, PWMGenPeriodGet(PWM1_BASE, PWM_GEN_1)*(_duty/2)); } }

9.编码器

同样地,编码器也是十分重要的内容,这里的驱动库实现了对于编码器的各种数值的获取。当然,这也是改编自学长的。

//QEI_Model.h /* * QEI_Model.h * * Created on: 2018年1月29日 * Author: bobo */ #ifndef QEI_MODEL_H_ #define QEI_MODEL_H_ typedef struct { uint32_t periph; uint32_t base; uint32_t swap_flag; uint32_t Qinterrupt; uint32_t gpio_periph; uint32_t gpio_port; uint8_t gpio_pin; uint32_t gpio_cfg_a; uint32_t gpio_cfg_b; int32_t dir; int32_t velocity; /* unit: pulse edge per second */ // int32_t Init_Pos; uint32_t Now_Pos; //0~1040 }user_qei_data_t; extern user_qei_data_t qei_data_array[2]; //extern void QEI_InitConfig(user_qei_data_t *qei_data); extern void QEI_Config(void); extern void QEI0_IRQHandler(void); #endif /* QEI_MODEL_H_ */ 
//QEI_Model.h #include <stdint.h> #include <stdbool.h> #include "inc/tm4c123gh6pm.h" #include "driverlib/sysctl.h" #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/qei.h" #include "inc/hw_memmap.h" #include "inc/hw_gpio.h" #include "driverlib/interrupt.h" #include "inc/hw_types.h" #include "QEI_Model.h" #define MOTOR_QEI_VEL_FREQ 10 bool Init_flag = true; //void QEI_InitConfig(user_qei_data_t *qei_data); void QEI_Config(void); user_qei_data_t qei_data_array[2] = { { //QEI0: D6,D7 .periph = SYSCTL_PERIPH_QEI0, .base = QEI0_BASE, .swap_flag = QEI_CONFIG_SWAP, .Qinterrupt = INT_QEI0, .gpio_periph = SYSCTL_PERIPH_GPIOD, .gpio_port = GPIO_PORTD_BASE, .gpio_pin = (GPIO_PIN_6 | GPIO_PIN_7), .gpio_cfg_a = GPIO_PD6_PHA0, .gpio_cfg_b = GPIO_PD7_PHB0, .dir = 1, .velocity = 0, .Init_Pos = 0, .Now_Pos = 0, }, { .periph = SYSCTL_PERIPH_QEI1, .base = QEI1_BASE, .swap_flag = QEI_CONFIG_NO_SWAP, .Qinterrupt = INT_QEI1, .gpio_periph = SYSCTL_PERIPH_GPIOC, .gpio_port = GPIO_PORTC_BASE, .gpio_pin = (GPIO_PIN_5 | GPIO_PIN_6), .gpio_cfg_a = GPIO_PC5_PHA1, .gpio_cfg_b = GPIO_PC6_PHB1, .dir = 1, .velocity = 0, .Init_Pos = 0, .Now_Pos = 0, }, }; void QEI_InitConfig(user_qei_data_t *qei_data) { // // Enable the QEI0 peripheral // SysCtlPeripheralEnable(qei_data->periph); // // Wait for the QEI0 module to be ready. // while(!SysCtlPeripheralReady(qei_data->periph)); // // Configure gpio // SysCtlPeripheralEnable(qei_data->gpio_periph); while(!SysCtlPeripheralReady(qei_data->gpio_periph)); if(qei_data->gpio_port == GPIO_PORTD_BASE && (qei_data->gpio_pin & GPIO_PIN_7)) { // // unlock PF0 // HWREG(GPIO_PORTD_BASE + GPIO_O_LOCK) = 0x4C4F434B; HWREG(GPIO_PORTD_BASE + GPIO_O_CR) |= GPIO_PIN_7; HWREG(GPIO_PORTD_BASE + GPIO_O_LOCK) = 0x00; } GPIOPinConfigure(qei_data->gpio_cfg_a); GPIOPinConfigure(qei_data->gpio_cfg_b); GPIOPinTypeQEI(qei_data->gpio_port, qei_data->gpio_pin); // // Configure the quadrature encoder to capture edges on both signals and // maintain an absolute position by resetting on index pulses. Using a // 1000 line encoder at four edges per line, there are 4000 pulses per // revolution; therefore set the maximum position to 3999 as the count // is zero based. //1040 -> 0x410 QEIConfigure(qei_data->base, (QEI_CONFIG_CAPTURE_A_B | QEI_CONFIG_NO_RESET | QEI_CONFIG_QUADRATURE | qei_data->swap_flag), 0x410); // // Enable the quadrature encoder. // QEIEnable(qei_data->base); // // Configure qei velocity // QEIVelocityConfigure(qei_data->base, QEI_VELDIV_1, SysCtlClockGet() / MOTOR_QEI_VEL_FREQ); QEIVelocityEnable(qei_data->base); QEIIntEnable(qei_data->base, QEI_INTDIR | QEI_INTTIMER); IntEnable(qei_data->Qinterrupt); //INT_QEI0 //IntRegister(INT_QEI0,QEI0_IRQHandler); } void QEI_Config(void) { // uint32_t i; // for(i = 0; i < sizeof(qei_data_array) / sizeof(qei_data_array[0]); i++) // { // QEI_InitConfig(qei_data_array + i); // SysCtlDelay(SysCtlClockGet()*10/3000); // } QEI_InitConfig(qei_data_array+0); SysCtlDelay(SysCtlClockGet()*5/3000); } void qei_irq_handler(int32_t QEInum) { uint32_t status = QEIIntStatus(qei_data_array[QEInum].base, true); QEIIntClear(qei_data_array[QEInum].base, status); if(status & QEI_INTTIMER) { qei_data_array[QEInum].velocity = QEIVelocityGet(qei_data_array[QEInum].base) * MOTOR_QEI_VEL_FREQ * QEIDirectionGet(qei_data_array[QEInum].base); //qei_data_array[QEInum].Now_Pos = QEIPositionGet(qei_data_array[QEInum].base); } if(status & QEI_INTDIR) { qei_data_array[QEInum].dir = -qei_data_array[QEInum].dir; } if(Init_flag) { qei_data_array[QEInum].Init_Pos = QEIPositionGet(qei_data_array[QEInum].base); Init_flag = false; } QEIPositionGet(qei_data_array[QEInum].base); QEIVelocityGet(qei_data_array[QEInum].base) * MOTOR_QEI_VEL_FREQ * QEIDirectionGet(qei_data_array[QEInum].base); } void QEI0_IRQHandler(void) { qei_irq_handler(0); }

10.定时器

定时器是每一个项目中都必然会用到的东西,这里写的定时器驱动库编写了定时器0和1的普通使用以及使用其溢出中断。

//timer.h # ifndef TIMER_H # define TIMER_H void Timer0_Int_Init();//定时器带溢出中断的初始化 void Timer1_Int_Init(); void Timer0BIntHandler();//定时器中断处理函数 void Timer1BIntHandler(); void Timer_Init();//定时器不带中断的初始化 #endif
//timer.c #include "timer.h" #include <stdint.h> #include <stdbool.h> #include "inc/hw_memmap.h" #include "inc/hw_ints.h" #include "driverlib/gpio.h" #include "driverlib/interrupt.h" #include "driverlib/pin_map.h" #include "driverlib/sysctl.h" #include "driverlib/timer.h" #include "driverlib/uart.h" #include "driverlib/rom_map.h" #include "driverlib/rom.h" #include "utils/uartstdio.h" #include "libraries/LED/led.h" int timer_flag=0; extern int mode; void Timer0_Int_Init() { SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerClockSourceSet(TIMER0_BASE, TIMER_CLOCK_SYSTEM); TimerPrescaleSet(TIMER0_BASE, TIMER_B, 16 - 1); TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_PERIODIC); TimerLoadSet(TIMER0_BASE, TIMER_B, ((SysCtlClockGet() / (TimerPrescaleGet(TIMER0_BASE, TIMER_B) + 1)) / 50) - 1); TimerIntRegister(TIMER0_BASE, TIMER_B, Timer0BIntHandler); TimerIntEnable(TIMER0_BASE, TIMER_TIMB_TIMEOUT); IntEnable(INT_TIMER0B); TimerEnable(TIMER0_BASE, TIMER_B); IntMasterEnable(); } void Timer0BIntHandler() { } void Timer_Init() { SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC_UP); TimerLoadSet(TIMER0_BASE, TIMER_A, 0xFFFFFFFF); IntDisable(INT_TIMER0A); TimerEnable(TIMER0_BASE, TIMER_A); IntMasterEnable(); } void Timer1_Int_Init() { SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); TimerClockSourceSet(TIMER1_BASE, TIMER_CLOCK_SYSTEM); TimerPrescaleSet(TIMER1_BASE, TIMER_B, 16 - 1); TimerConfigure(TIMER1_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_PERIODIC); TimerLoadSet(TIMER1_BASE, TIMER_B, ((SysCtlClockGet() / (TimerPrescaleGet(TIMER1_BASE, TIMER_B) + 1)) / 500) - 1); TimerIntRegister(TIMER1_BASE, TIMER_B, Timer1BIntHandler); TimerIntEnable(TIMER1_BASE, TIMER_TIMB_TIMEOUT); IntEnable(INT_TIMER1B); TimerEnable(TIMER1_BASE, TIMER_B); IntMasterEnable(); } void Timer1BIntHandler() { }

11.串口

串口也是很重要的东西啊!我写的串口库包括了普通模式和带接受中断的模式,适用于不同的情景下。我写的这个库包括了串口0和串口1的初始化,基本上能满足大部分情况了。

//uart.h #ifndef UART_H #define UART_H #include <stdint.h> #include <stdbool.h> void UART0_Init(uint32_t ui32Baud);//串口0初始化 void UART0_Int_Init(uint32_t ui32Baud);//串口0中断初始化 void UART0IntHandler();//串口0中断处理函数 bool UARTDataDeal(char *pui8Data);//串口接收信息处理函数 void UART1_Init(uint32_t ui32Baud);//串口1初始化 void UART1_Int_Init(uint32_t ui32Baud);//串口1中断初始化 void UART1IntHandler();//串口1中断处理函数 #endif
//uart.c #include "uart.h" #include <stdint.h> #include <stdbool.h> #include <stdlib.h> #include <string.h> #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" #include "inc/hw_gpio.h" #include "driverlib/pin_map.h" #include "driverlib/gpio.h" #include "driverlib/sysctl.h" #include "driverlib/interrupt.h" #include "driverlib/uart.h" #include "utils/uartstdio.h" #include "../LED/led.h" #include "../delay/delay.h" int speed=100; extern int mode; void UART0_Init(uint32_t ui32Baud) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioConfig(0, ui32Baud, SysCtlClockGet()); } void UART0_Int_Init(uint32_t ui32Baud) { SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioConfig(0, ui32Baud, SysCtlClockGet()); UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), ui32Baud,(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |UART_CONFIG_PAR_NONE)); UARTFIFOEnable(UART0_BASE); UARTFIFOLevelSet(UART0_BASE,UART_FIFO_TX2_8,UART_FIFO_TX4_8); UARTIntEnable(UART0_BASE, UART_INT_RX); IntPrioritySet (INT_UART0, 0xE0); // UARTIntRegister(UART0_BASE, UART0IntHandler); IntEnable(INT_UART0); IntMasterEnable(); } void UART0IntHandler() { char ui8RxBuffer[10]={0}; uint8_t i = 0; UARTIntClear(UART0_BASE, UARTIntStatus(UART0_BASE, true)); delay_ms(1); while(UARTCharsAvail(UART0_BASE)) { ui8RxBuffer[i++] =(uint8_t)UARTCharGetNonBlocking(UART0_BASE); if(ui8RxBuffer[i-1]=='\n') { ui8RxBuffer[i-1]=0; i=0; break; } } UARTDataDeal(ui8RxBuffer); } bool UARTDataDeal(char *pui8Data) { char input[10]={0}; int temp=100; input[0]=pui8Data[0]; input[1]=pui8Data[1]; input[2]=pui8Data[2]; input[3]=pui8Data[3]; input[4]=pui8Data[4]; input[5]=pui8Data[5]; input[6]=pui8Data[6]; input[7]=pui8Data[7]; if(mode==1) { if(input[4]=0,!strcmp(input,"dir+")) { UARTprintf("back:dir+\n"); speed=abs(speed); return true; } else if(input[4]=0,!strcmp(input,"dir-")) { UARTprintf("back:dir-\n"); speed=-1*abs(speed); return true; } else if(input[0]=='s'&&input[1]=='p'&&input[2]=='e'&&input[3]=='e') { temp=atoi(input+5); if(temp>180||temp<40) { temp=100; UARTprintf("wrong!\n"); } if(speed>0)speed=temp; else speed=-1*temp; UARTprintf("Speed=%d\n",speed); return true; } } UARTprintf("wrong enter!\n"); return false; } void UART1_Init(uint32_t ui32Baud) { SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); GPIOPinConfigure(GPIO_PB0_U1RX); GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0); GPIOPinConfigure(GPIO_PB1_U1TX); GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_1); UARTConfigSetExpClk(UART1_BASE, SysCtlClockGet(), 115200,(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |UART_CONFIG_PAR_NONE)); UARTEnable(UART1_BASE); } void UART1_Int_Init(uint32_t ui32Baud) { HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY; HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x1; SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1); GPIOPinConfigure(GPIO_PB0_U1RX); GPIOPinConfigure(GPIO_PB1_U1TX); GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioConfig(0, ui32Baud, SysCtlClockGet()); UARTConfigSetExpClk(UART1_BASE, SysCtlClockGet(), ui32Baud,(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |UART_CONFIG_PAR_NONE)); UARTFIFOEnable(UART1_BASE); UARTFIFOLevelSet(UART1_BASE,UART_FIFO_TX2_8,UART_FIFO_TX2_8); UARTIntEnable(UART1_BASE, UART_INT_RX); IntPrioritySet (INT_UART1, 0xE0); UARTIntRegister(UART1_BASE, UART1IntHandler); IntEnable(INT_UART1); IntMasterEnable(); } void UART1IntHandler() { }

以上就是这次tm4基础学习中我配置的驱动库啦~希望我能在接下来的学习中学到更多知识!

发布者 | 2019-07-02T02:56:16+08:00 二月 12th, 2019|单片机, 学习笔记|0条评论

关于作者

坚强大概——并不是指的的结果,而是迈向某个目标的过程吧。

发表评论

博客作者

坚强大概——并不是指的的结果,而是迈向某个目标的过程吧。

标签云

博客统计

  • 4,039 点击次数
召唤伊斯特瓦尔