collab/mm20
git clone https://git.julia.blue/collab/mm20
12fc04d
adc
1#include <launchpad.h> 2#include <math.h> 3#include <stdint.h> 4#include <stdio.h> 5#include <tivaware/adc.h> 6#include <tivaware/gpio.h> 7#include <tivaware/hw_ints.h> 8#include <tivaware/hw_memmap.h> 9#include <tivaware/pin_map.h> 10#include <tivaware/pwm.h> 11#include <tivaware/rom.h> 12#include <tivaware/sysctl.h> 13#include <tivaware/timer.h> 14 15extern void disable_interrupts (void ); 16extern void enable_interrupts (void ); 17 18volatile uint32_t delay_timer ; 19 20void systick_handler (void ) {-- delay_timer ; } 21 22void wait_ms (uint32_t t ) { 23delay_timer = t ; 24ROM_SysTickPeriodSet (80000 - 1 ); 25ROM_SysTickIntEnable (); 26while (delay_timer != 0 ) { 27 } 28ROM_SysTickIntDisable (); 29} 30 31void blink_success (void ) { 32led_write (GREEN_LED , true); 33wait_ms (250 ); 34led_write (GREEN_LED , false); 35} 36 37void timer0_init () { 38ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_TIMER0 ); 39ROM_TimerConfigure (TIMER0_BASE ,TIMER_CFG_PERIODIC ); 40ROM_TimerLoadSet (TIMER0_BASE ,TIMER_A ,1600000 );// 100Hz 41ROM_IntEnable (INT_TIMER0A ); 42ROM_TimerIntEnable (TIMER0_BASE ,TIMER_TIMA_TIMEOUT ); 43ROM_TimerEnable (TIMER0_BASE ,TIMER_BOTH ); 44} 45 46void adc0_init () { 47ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_ADC0 );// enable ADC0 48ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOE );// enable for AIN0 on E3 49ROM_GPIOPinTypeADC (GPIO_PORTE_BASE ,GPIO_PIN_3 ); 50ROM_ADCSequenceConfigure (ADC0_BASE ,3 ,ADC_TRIGGER_PROCESSOR ,0 ); 51ROM_ADCSequenceStepConfigure (ADC0_BASE ,3 ,0 , 52ADC_CTL_CH0 |ADC_CTL_IE |ADC_CTL_END ); 53 54ROM_ADCSequenceEnable (ADC0_BASE ,3 ); 55 56ROM_ADCIntClear (ADC0_BASE ,3 ); 57} 58 59void pwm0_init (int freq ,int duty ) { 60ROM_SysCtlPWMClockSet (SYSCTL_PWMDIV_8 ); 61ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_PWM0 ); 62ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOB ); 63ROM_GPIOPinConfigure (GPIO_PB6_M0PWM0 ); 64ROM_GPIOPinTypePWM (GPIO_PORTB_BASE ,GPIO_PIN_6 ); 65ROM_PWMGenConfigure (PWM0_BASE ,PWM_GEN_0 , 66PWM_GEN_MODE_UP_DOWN |PWM_GEN_MODE_NO_SYNC ); 67ROM_PWMGenPeriodSet (PWM0_BASE ,PWM_GEN_0 ,100000 ); 68ROM_PWMPulseWidthSet (PWM0_BASE ,PWM_OUT_0 , 69ROM_PWMGenPeriodGet (PWM0_BASE ,PWM_GEN_0 ) /duty ); 70ROM_PWMOutputState (PWM0_BASE ,PWM_OUT_0_BIT , true); 71ROM_PWMGenEnable (PWM0_BASE ,PWM_GEN_0 ); 72} 73 74static uint32_t pui32ADC0Value [1 ]; 75void adc0_read () { 76ROM_ADCProcessorTrigger (ADC0_BASE ,3 );// trigger ADC conversion 77while ( 78 !ROM_ADCIntStatus (ADC0_BASE ,3 , false)) {// wait for conversion to finish 79 } 80ROM_ADCIntClear (ADC0_BASE ,3 ); 81ROM_ADCSequenceDataGet (ADC0_BASE ,3 ,pui32ADC0Value );// read AIN0 E3 82} 83 84void timer0a_handler (void ) { 85ROM_TimerIntClear (TIMER0_BASE ,TIMER_A ); 86ROM_GPIOPinWrite (GPIO_PORTA_BASE ,GPIO_PIN_7 , 87 ~ROM_GPIOPinRead (GPIO_PORTA_BASE ,GPIO_PIN_7 )); 88adc0_read (); 89printf ("%4d \n\r" ,pui32ADC0Value [0 ]); 90} 91 92int main (void ) { 93ROM_SysCtlClockSet (SYSCTL_SYSDIV_2_5 |SYSCTL_USE_PLL |SYSCTL_XTAL_16MHZ | 94SYSCTL_OSC_MAIN ); 95ROM_SysTickEnable (); 96launchpad_init (); 97ROM_GPIOPinTypeGPIOOutput (GPIO_PORTA_BASE ,GPIO_PIN_7 ); 98// timer0_init(); 99// adc0_init(); 100pwm0_init (100 ,2 ); 101while (1 ) { 102blink_success (); 103 104printf ("%d\n\r" ,ROM_PWMGenPeriodGet (PWM0_BASE ,PWM_GEN_0 )); 105wait_ms (100 ); 106 } 107}