Quantcast
Channel: Forums - Recent Threads
Viewing all articles
Browse latest Browse all 262198

PWM encoding 8 bits command

$
0
0

Hello,

 I'm using lm6965. I am trying to modulate  8 bit data using variable duty cycle(for a logical "1" bit the pulse width is 75% of the bit period while for a logical "0" bit, the pulse width is 25% of the bit period ). After power up/hardware reset, the program works fine when modulating the first data byte ( in the attached code the data byte comes from 1000100000  . The problem appears when I want to process a second data byte. In this case the LSB just doesn't appear while the other bits are modulated as expected. 

Bellow is my code that I'm implementing: 

#include <stdint.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
#include "driverlib/pwm.h"
#include "inc/hw_pwm.h"
#include "utils/uartstdio.h"

//#define PWM_GEN_BADDR(_mod_, _gen_) \
// ((_mod_) + (_gen_))
// acest program este facut sa mearga pe uC lm3s6965

void PWMIntHandler(void);
void InitConsole(void);
void initPWM(void);
void startPWM(void);

#define PWM_DATA_BITS 9

unsigned long ulPeriod;
unsigned char myData = 100;
//unsigned char counter = 0;
unsigned long pwm_freq = 100000;
unsigned char duty_Cycle = 4;
unsigned long pwm_dataVec[PWM_DATA_BITS + 1];
volatile unsigned char pwm_data_index=2;

unsigned long low_duty_cycle;
unsigned long high_duty_cycle;

unsigned char i;

unsigned char numberOfRuns=0;

int main(void)
{
SysCtlDelay(4000000);

SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN| SYSCTL_XTAL_8MHZ);

InitConsole();
initPWM();

// for( i = 0; i < PWM_DATA_BITS - 1; i++ )
// {
// if (i % 2)
// pwm_dataVec[i] = high_duty_cycle;
// else
// pwm_dataVec[i] = low_duty_cycle;
// }
// pwm_dataVec[PWM_DATA_BITS]=1;
pwm_dataVec[0]=high_duty_cycle;
pwm_dataVec[1]=low_duty_cycle;
pwm_dataVec[2]=low_duty_cycle;
pwm_dataVec[3]=high_duty_cycle;
pwm_dataVec[4]=high_duty_cycle;
pwm_dataVec[5]=low_duty_cycle;
pwm_dataVec[6]=high_duty_cycle;
pwm_dataVec[7]=low_duty_cycle;
pwm_dataVec[8]=high_duty_cycle;
pwm_dataVec[9]=low_duty_cycle;

// N = (1 / f) * SysClk. Where N is the
// function parameter, f is the desired frequency,
// PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, ulPeriod * 2 / duty_Cycle);
// PWMGenEnable(PWM_BASE, PWM_GEN_1);
// PWMOutputState(PWM_BASE, PWM_OUT_2_BIT, true);

startPWM();

while (1)
{
UARTCharGet(UART0_BASE);
if (pwm_data_index > 8)
{
UARTprintf("a");
startPWM();
}
}
}

void PWMIntHandler(void) {
unsigned long ulStatus;

ulStatus = PWMGenIntStatus(PWM_BASE, PWM_GEN_1, true);
PWMGenIntClear(PWM_BASE, PWM_GEN_1, ulStatus);

if (pwm_data_index > (PWM_DATA_BITS - 1))
{
// PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, pwm_dataVec[0]);
// PWMGenIntTrigDisable(PWM_BASE, PWM_GEN_1, PWM_INT_CNT_AD);
PWMGenDisable(PWM_BASE, PWM_GEN_1); // to minimize the glitch of the final PWM pulse++

return;
}

//Note: Will not overflow because vector has one dummy byte
//Note: This is faster because of less code to run in interrupt

PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, pwm_dataVec[pwm_data_index]);

if (pwm_data_index > (PWM_DATA_BITS - 2)) //for the last bit enter interrupt on compare event (output goes low)
{
PWMGenIntTrigEnable(PWM_BASE, PWM_GEN_1, PWM_INT_CNT_AD);
}

++pwm_data_index;

}

//*****************************************************************************
//
// This function sets up UART0 to be used for a console to display information
// as the example is running.
//
//*****************************************************************************
void InitConsole(void) {
//
// Enable GPIO port A which is used for UART0 pins.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

//
// Configure the pin muxing for UART0 functions on port A0 and A1.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
//
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);

//
// Select the alternate (UART) function for these pins.
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

//
// Initialize the UART for console I/O.
//
UARTStdioInit(0);
}

void initPWM()
{

SysCtlPWMClockSet(SYSCTL_PWMDIV_1); //set PWM clock
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM); //enable PWM peripheric

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //GPIO port B bit 0
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //GPIO port F bit 0

GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_0);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0);

// UARTprintf("%x \n",SysCtlPWMClockGet());
// UARTprintf("%x \n",SysCtlClockGet());

IntEnable(INT_PWM1);
PWMIntEnable(PWM_BASE, PWM_INT_GEN_1);
PWMGenIntTrigEnable(PWM_BASE, PWM_GEN_1, PWM_INT_CNT_ZERO);

ulPeriod = SysCtlClockGet() / pwm_freq;

low_duty_cycle = ulPeriod / 4;
high_duty_cycle = ulPeriod * 3 / 4;
//
// UARTprintf("%x \n",low_duty_cycle);
// UARTprintf("%x \n",high_duty_cycle);

PWMGenConfigure(PWM_BASE, PWM_GEN_1,
PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM_BASE, PWM_GEN_1, ulPeriod);
IntMasterEnable();


}

void startPWM()
{

PWMGenIntTrigDisable(PWM_BASE, PWM_GEN_1, PWM_INT_CNT_AD);
PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, pwm_dataVec[0]);
// SysCtlDelay(10);
// UARTprintf("%x\n", PWMPulseWidthGet(PWM_BASE, PWM_OUT_2));
// UARTprintf("%x\n\n", pwm_dataVec[0]);

// HWREG(((PWM_BASE) + (PWM_GEN_1)) + PWM_O_X_COUNT) =1;

PWMGenEnable(PWM_BASE, PWM_GEN_1);
PWMOutputState(PWM_BASE, PWM_OUT_2_BIT, true);

//while(HWREG(((PWM_BASE) + (PWM_GEN_1)) + PWM_O_X_COUNT) == 0);
if (numberOfRuns != 0)
PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, pwm_dataVec[0]);
// UARTprintf("%x \n",numberOfRuns);
numberOfRuns=1;

// SysCtlDelay(10);
PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, pwm_dataVec[1]);

pwm_data_index=2;

}

Thank you 


Viewing all articles
Browse latest Browse all 262198

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>