模式一&模式二:單次計時&周期計時/*******************************************
開發壞境:CCSv5.4
開發板:TIVA C Launchpad(TM4C123GH6PM)
程序功能:16位定時器,單次定時模式和周期性定時模式
程序說明:
編程者:Linchpin
********************************************/
#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
//5分頻,使用PLL,外部晶振16M,system時鍾源選擇 main osc。系統時鍾40MHZ
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
//使能GPIOF外設
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
//使能TIMER0
//TimerConfigure(TIMER0_BASE, TIMER_CFG_ONE_SHOT);//單次計數模式
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);//周期性計數模式
TimerLoadSet(TIMER0_BASE, TIMER_A,SysCtlClockGet() / 10 - 1);//計數頻率10HZ
IntEnable(INT_TIMER0A);//NVIC
//使能TIMER0A
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
//TIMEOUT標志位觸發中斷
IntMasterEnable();
//master interrupt enable API for all interrupts
TimerEnable(TIMER0_BASE, TIMER_A);
//TIMER0A開始計數,當計數值等於TimerLoadSet,觸發中斷
while(1)
{
}
}
void Timer0IntHandler(void)
{
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
//清除標志位
if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2))
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
}
else
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 4);
}
}
模式三:計數捕獲
/*******************************************
開發壞境:CCSv5.4
開發板:TIVA C Launchpad(TM4C123GH6PM)
程序功能:16位定時器,計數捕獲模式,遞增計數模式
程序說明:捕獲引腳PB6,當捕獲邊沿數達到Match時產生中斷
編程者:Linchpin
********************************************/
#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
//5分頻,使用PLL,外部晶振16M,system時鍾源選擇 main osc。系統時鍾40MHZ
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
//使能TIMER0 32位(TIMER0A16位+TIMER0B16位)
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//使能GPIOF和GPIOC外設
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_6);
GPIOPinConfigure(GPIO_PB6_T0CCP0); //#define GPIO_PC4_WT0CCP0 0x00021007
GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_6);
//計數捕獲模式,上升沿捕獲,Two half-width timers
TimerConfigure(TIMER0_BASE,TIMER_CFG_A_CAP_COUNT_UP|TIMER_CFG_SPLIT_PAIR);
//捕獲模式,A定時器,上升沿捕獲,增計數模式,到達匹配值后可自動清零
TimerControlEvent(TIMER0_BASE,TIMER_A,TIMER_EVENT_POS_EDGE);
//TimerLoadSet(TIMER0_BASE, TIMER_A, 6);//溢出值6
TimerMatchSet(TIMER0_BASE, TIMER_A, 3);//匹配值3
IntEnable(INT_TIMER0A);
//使能TIMER0A
TimerIntEnable(TIMER0_BASE, TIMER_CAPA_MATCH);
//定時器A捕獲事件觸發中斷
IntMasterEnable();
//master interrupt enable API for all interrupts
TimerEnable(TIMER0_BASE, TIMER_A);
//TIMER0A開始計數,當計數值等於TimerLoadSet,觸發中斷
while(1)
{
}
}
void Timer0IntHandler(void)
{
TimerIntClear(TIMER0_BASE,TIMER_CAPA_MATCH);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 14);
SysCtlDelay(SysCtlClockGet() / 30);//100ms
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
}
模式四:計時捕獲
/*******************************************
開發壞境:CCSv5.4
開發板:TIVA C Launchpad(TM4C123GH6PM)
程序功能:16位定時器,計時捕獲模式
程序說明:捕獲引腳PB6
編程者:Linchpin
********************************************/
#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
//5分頻,使用PLL,外部晶振16M,system時鍾源選擇 main osc。系統時鍾40MHZ
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
//使能TIMER0 32位(TIMER0A16位+TIMER0B16位)
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//使能GPIOF和GPIOC外設
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_6);
GPIOPinConfigure(GPIO_PB6_T0CCP0); //#define GPIO_PC4_WT0CCP0 0x00021007
GPIOPinTypeTimer(GPIO_PORTB_BASE, GPIO_PIN_6);
TimerConfigure(TIMER0_BASE,TIMER_CFG_A_CAP_TIME_UP|TIMER_CFG_SPLIT_PAIR);//計時捕獲模式,上升沿捕獲
TimerControlEvent(TIMER0_BASE,TIMER_A,TIMER_EVENT_POS_EDGE);
//捕獲模式,A定時器,上升沿捕獲
IntEnable(INT_TIMER0A);
//使能TIMER0A
TimerIntEnable(TIMER0_BASE, TIMER_CAPA_EVENT);
//定時器A捕獲事件觸發中斷
IntMasterEnable();
//master interrupt enable API for all interrupts
TimerEnable(TIMER0_BASE, TIMER_A);
//TIMER0A開始計數,當計數值等於TimerLoadSet,觸發中斷
while(1)
{
}
}
void Timer0IntHandler(void)
{
TimerIntClear(TIMER0_BASE,TIMER_CAPA_EVENT);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
SysCtlDelay(SysCtlClockGet() / 30);//100ms
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 14);
}
方式一:GPIO模擬SPI通信
/*******************************************
開發壞境:CCSv5.4
開發板:TIVA C Launchpad(TM4C123GH6PM)
程序功能:DAC_TLV5618 輸出正弦波
程序說明:1.DIN——PC6 2.SCLK——PC5 3.CS——PC4 4.OUTA 5.AGND 6.REF 7.OUTB 8.VDD
編程者:Linchpin
********************************************/
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/fpu.h"
#include "driverlib/sysctl.h"
#include "grlib/grlib.h"
#include "driverlib/gpio.h"
#include "inc/hw_gpio.h"
#define uchar unsigned char
#define uint unsigned int
//void CS(uchar i);
//void SCLK(uchar i);
//void DIN(uchar i);
void init_DAC(void);
void DA_conver(uint temp);
void Write_A(float out_volt);
int VoltToData(float out_volt) ;
void delay_Nms(uint n);
float sinx_128[]={0.299734,0.313719,0.327637,0.341454,0.355137,0.368653,0.381968,0.395052,0.407873,0.420399,0.432601,0.444449,0.455914,0.466969,0.477588,0.487745,0.497414,0.506574,0.515202,0.523277,0.530779,0.537692,0.543997,0.549680,0.554727,0.559126,0.562866,0.565939,0.568336,0.570053,0.571084,0.571429,0.571084,0.570053,0.568336,0.565939,0.562866,0.559126,0.554727,0.549680,0.543997,0.537692,0.530780,0.523277,0.515202,0.506575,0.497415,0.487745,0.477589,0.466970,0.455915,0.444449,0.432601,0.420400,0.407873,0.395053,0.381969,0.368653,0.355138,0.341455,0.327638,0.313720,0.299734,0.285715,0.271696,0.257710,0.243792,0.229975,0.216292,0.202777,0.189461,0.176377,0.163556,0.151030,0.138829,0.126981,0.115515,0.104460,0.093841,0.083684,0.074015,0.064855,0.056227,0.048152,0.040649,0.033737,0.027432,0.021749,0.016702,0.012303,0.008563,0.005490,0.003093,0.001376,0.000344,0.000005,0.000344,0.001376,0.003092,0.005490,0.008562,0.012302,0.016701,0.021748,0.027431,0.033736,0.040648,0.048151,0.056226,0.064853,0.074013,0.083683,0.093839,0.104458,0.115513,0.126979,0.138827,0.151028,0.163554,0.176375,0.189459,0.202774,0.216290,0.229973,0.243790,0.257708,0.271693,0.285713,};
uint num;
void main(void)
{
//使能FPU
FPUEnable();
FPULazyStackingEnable();
SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_XTAL_10MHZ|SYSCTL_OSC_MAIN);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);//使能GPIO外設
SysCtlGPIOAHBEnable(GPIO_PORTC_BASE);//掛在AHB高性能總線上
GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6);
init_DAC();
while(1)
{
Write_A(sinx_128[num++]);
if(num>=128) num=0;
}
}
//片選信號端,低電平有效,PC4
/*void CS(uchar i)
{
if(i) GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4,GPIO_PIN_4);
else GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4,~GPIO_PIN_4);
}
//同步串行時鍾輸入,PC5
void SCLK(uchar i)
{
if(i) GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,GPIO_PIN_5 );
else GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,~GPIO_PIN_5);
}
//串行數據輸入端,串行時鍾上升沿輸入,PC6
void DIN(uchar i)
{
if(i) GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6,GPIO_PIN_6 );
else GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6,~GPIO_PIN_6);
}*/
/******初始化DAC***************/
void init_DAC(void)
{
GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4,GPIO_PIN_4);//CS(1);
GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,~GPIO_PIN_5);//SCLK(0);
GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6,~GPIO_PIN_6);//DIN(0);
}
void DA_conver(uint temp)
{
uchar i;
GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4,~GPIO_PIN_4);//CS(0);
GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,GPIO_PIN_5 );//SCLK(1);
for(i=0;i<16;i++)
{
if( (temp & 0x8000) ==0) GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6,~GPIO_PIN_6);
else GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6,GPIO_PIN_6);
GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,~GPIO_PIN_5 );
//;
temp<<=1;
GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,GPIO_PIN_5 );
}
GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_4,GPIO_PIN_4);
}
void Write_A(float out_volt)
{
//int Data_A;
//Data_A=VoltToData(out_volt);
//A通道
DA_conver(0xc000|(0x0fff&VoltToData(out_volt)));
}
int VoltToData(float out_volt) //說明ref為參考源 out_volt為輸出模擬電壓值
{
int temp2;
temp2=(int)((out_volt*4096)/(2*3.3));
return(temp2&0xfff);
}//將電壓值轉換為對應的12位數字量函數
方式二:硬件SPI通信(速度比GPIO模擬快很多)
/*******************************************
開發壞境:CCSv5.4
開發板:TIVA C Launchpad(TM4C123GH6PM)
程序功能:TLV5618 SPI通信(TI模式)
程序說明:1.DIN——PF1 2.SCLK——PF2 3.CS——PF3
DAC采樣速率:95KHZ
編程者:Linchpin
********************************************/
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "inc/hw_ssi.h"
#include "grlib/grlib.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/ssi.h"
#include "driverlib/rom.h"
#include "driverlib/pin_map.h"
#define uchar unsigned char
#define uint unsigned int
void DAC_Write_A(float out_volt);
void main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |SYSCTL_XTAL_10MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
//SysCtlGPIOAHBEnable(GPIO_PORTF_BASE);//掛在AHB高性能總線上
GPIOPinConfigure(GPIO_PF2_SSI1CLK);
GPIOPinConfigure(GPIO_PF3_SSI1FSS);
GPIOPinConfigure(GPIO_PF1_SSI1TX);
GPIOPinTypeSSI(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_2);
//SSI1_BASE/時鍾頻率/時鍾協議,moto代表SPI協議/工作模式:主機模式/位速率4M/數據幀位數16
//SSIConfigSetExpClk(SSI1_BASE, SysCtlClockGet(), SSI_FRF_MOTO_MODE_0 , SSI_MODE_MASTER, 25000000, 16);
SSIConfigSetExpClk(SSI1_BASE, SysCtlClockGet(), SSI_FRF_TI , SSI_MODE_MASTER, 25000000, 16);
SSIEnable(SSI1_BASE);
while(1)
{
DAC_Write_A(1);
//SysCtlDelay(SysCtlClockGet()/3000);
DAC_Write_A(2);
//SysCtlDelay(SysCtlClockGet()/3000);
}
}
void DAC_Write_A(float out_volt)
{
//(uint)(out_volt*4095)/(2*3.281) 先將電壓值轉化為0~4095的整數
//0x0fff& 轉化成16位二進制數,后12位為數據位
//0xc000| 前面加上4位為1100(命令位):快速模式,正常功耗,寫數據到A通道
SSIDataPut(SSI1_BASE, 0xc000|(0x0fff&((uint)((out_volt*4095)/(2*3.281)))));
while(SSIBusy(SSI1_BASE)) ;//等待發送完畢
}