标签:
模式一&模式二:单次计时&周期计时/*******************************************
开发坏境: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)) ;//等待发送完毕
}
? ?
标签:
原文地址:http://www.cnblogs.com/zhumeng321/p/4480176.html