Hi all,
I am using tiva c to toggle a pin on for 100us and turn off via serial input(interrupt by UART input). However, it does not toggle every time when the delay or gap is less than 100us. It works fine for 500us. I am wondering what causes the issue here. I have attached two pictures for you guys to compare.
100us delay
As you can see, when the delay is 100us, it simply skips some pulses while at 500us it generates all the pulses.
Here is my code.
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
//uart
#include "inc/hw_types.h"
#include "driverlib/pin_map.h"
#include "driverlib/uart.h"
//interrupt
#include "inc/hw_ints.h"
#include "driverlib/interrupt.h"
//PWM
#include "driverlib/pwm.h"
#include "inc/hw_gpio.h"
#include "driverlib/rom.h"
#define PWM_FREQUENCY 1// 10KHz, 100us
void UARTIntHandler(void) {
uint32_t ui32Status;
uint8_t ui8PinData=2;
ui32Status = UARTIntStatus(UART0_BASE, true); //get interrupt status
UARTIntClear(UART0_BASE, ui32Status); //clear the asserted interrupts
if(UARTCharsAvail(UART0_BASE)){
//PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT, true);
//SysCtlDelay(100); //50us for 3.125MHz SysCtlDelay(1)==1us
//PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT, false);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1| GPIO_PIN_2| GPIO_PIN_3, ui8PinData);
SysCtlDelay(500);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x00);
UARTCharPut(UART0_BASE,UARTCharGet(UART0_BASE));
}
}
int main(void) {
uint32_t ui32ADC0Value[4];
volatile float ui32Avg;
volatile float ui32Voltage;
volatile char print[7];
volatile char power[1];
volatile uint32_t ui32Load;
volatile uint32_t ui32PWMClock;
//Set the clock
SysCtlClockSet(
SYSCTL_SYSDIV_64 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN
| SYSCTL_XTAL_16MHZ); //400MHz / 2 (PLL) /64 = 3.125MHZ
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); //Enable ADC0
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); //Enable GPI0E
ADCReferenceSet(ADC0_BASE, ADC_REF_INT); //Set reference to the internal reference
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_5); //Configure GPIO Port E Pin 5 as ADC
ADCSequenceDisable(ADC0_BASE, 1); //It is always a good practice to disable ADC prior //to usage ,else the ADC may not be accurate // due to previous initializations
ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0); //Use the 1st Sample sequencer
ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_CH8);
ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_CH8);
ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_CH8);
ADCSequenceStepConfigure(ADC0_BASE, 1, 3,
ADC_CTL_CH8 | ADC_CTL_IE | ADC_CTL_END);
//Configure ADC to read from channel 8 ,trigger the interrupt to end data capture //
ADCSequenceEnable(ADC0_BASE, 1); //Enable the ADC
//UART
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);
UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 256000,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
//Interrupt
IntMasterEnable();
IntEnable(INT_UART0);
UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);
//toggle
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
//PWM
//SysCtlPWMClockSet(SYSCTL_PWMDIV_1); // 3.125MHz
//SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0);
//GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0);
//GPIOPinConfigure(GPIO_PD0_M1PWM0);
//ui32PWMClock = SysCtlClockGet();
//ui32Load = (ui32PWMClock / PWM_FREQUENCY) - 1;
//PWMGenConfigure(PWM1_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN);
//PWMGenPeriodSet(PWM1_BASE, PWM_GEN_0, ui32Load);
//PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, ui32Load); //100%
//PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT, false);
//PWMGenEnable(PWM1_BASE, PWM_GEN_0);
while (1) {
ADCIntClear(ADC0_BASE, 1); //Clear interrupt to proceed to data capture
ADCProcessorTrigger(ADC0_BASE, 1); //Ask processor to trigger ADC
while (!ADCIntStatus(ADC0_BASE, 1, false)) { //Do nothing until interrupt is triggered
}
ADCSequenceDataGet(ADC0_BASE, 1, ui32ADC0Value); //pui32ADC0Value is the value read
ui32Avg = (ui32ADC0Value[0] + ui32ADC0Value[1] + ui32ADC0Value[2]
+ ui32ADC0Value[3]) / 4;
ui32Voltage = (ui32Avg / 4095) * 3.3;
sprintf(print, "%f", ui32Voltage);
UARTCharPut(UART0_BASE, print[0]);
UARTCharPut(UART0_BASE, print[1]);
UARTCharPut(UART0_BASE, print[2]);
UARTCharPut(UART0_BASE, print[3]);
UARTCharPut(UART0_BASE, print[4]);
UARTCharPut(UART0_BASE, print[5]);
UARTCharPut(UART0_BASE, print[6]);
UARTCharPut(UART0_BASE, '\n');
UARTCharPut(UART0_BASE, '\r');
//SysCtlDelay(10);
}
}
Thanks