Reputation: 1
I am trying to actuate individual servo actuators (S51 Micro Servo) using the onboard PWM modules in a delay sequence. The Tiva datasheet states it can be done but I am unable to figure it out. I currently have PWM1 module set up using generators 0-3 and only 1 output for each generator. I should be able to operate 4 servos on PWM1 and 4 on PWM0 but I am very new to the programming world and not sure how to proceed. I am programming a Texas Instrument Tiva TM4C123GXL Launchpad ARM CORTEX-M4 using Keil uVision with C. I will attach my current code in hopes that someone will be able to point me in the right direction. Thank you in advance
`#include "TM4C123GH6PM.h"`
`#include <stdint.h>`
`#include "TM4C123.h"`
`#include <stdio.h>`
`#include "hw_types.h"`
`#include "hw_pwm.h"`
`void delayMs(int n);`
`void Bell_01();`
`void Bell_02();`
`void Bell_03();`
`void Bell_04();`
`void Bell_05();`
`void Bell_06();`
`void Bell_07();`
`void Bell_08();`
`int main(void)`
`{`
`SYSCTL->RCC = 0x06500540; // Set the frequency to 15.38MHz`
`// Enable Peripheral Clocks
SYSCTL->RCGCPWM |= 3; // Enable clock to PWM0 and PWM1
SYSCTL->RCGCGPIO |= 0x3B; // Enable clock to PORTA, PORTB, PORTD, PORTE, PORTF`
`SYSCTL->RCC &= 0x001A0000; // 32 pre-divide for PWM clock, 15.38MHz/32 = 480625/50 = 9612Hz`
`//GPIOA->AFSEL = 0x40; // PA6 uses alternate function
//GPIOA->PCTL &= ~0x0F000000; // Make pin A6 PWM1M2 output pin
//GPIOA->PCTL |= 0x05000000;
//GPIOA->DEN |= 0x40; // Set pin A6 to digital`
`GPIOB->AFSEL = 0x80; // PB7 uses alternate function
GPIOB->PCTL &= ~0xF0000000; // Make pin B7 PWM0M1 output pin
GPIOB->PCTL |= 0x40000000;
GPIOB->DEN |= 0x80;`
`GPIOD->AFSEL = 0x02; // PD1 uses alternate function
GPIOD->PCTL &= ~0x000000F0; // Make pin D1 PWM1M1 output pin
GPIOD->PCTL |= 0x00000050;
GPIOD->DEN = 0x02; // Set pin D1 to digital`
`GPIOE->AFSEL = 0x20; // PE5 uses alternate function
GPIOE->PCTL &= ~0x00F00000; // Make pin E5 PWM1M3 output pin
GPIOE->PCTL |= 0x00500000;
GPIOE->DEN |= 0x20; // Set pin E5 to digital`
`GPIOF->AFSEL = 0x0A; // PF1 & PF3 uses alternate function
GPIOF->PCTL &= ~0x0000F0F0; // Make pin F1 PWM1M5 & F3 PWM1M7 output pin
GPIOF->PCTL |= 0x00005050;
GPIOF->DEN |= 0x0A; // Set pin F1 & F3 to digital`
`PWM1->_0_CTL = 0x00; // Stop counter for proper activation
PWM1->_0_GENB = 0x0000008C; // M1PWM1 output set when reloaded, clear when matched with PWMCMPA (8250)
PWM1->_0_LOAD = 9612; // Set load value for 1kHz 16MHz/16000)
PWM1->_0_CMPA = 8250; // Set duty cycle to %
PWM1->_0_CTL = 0x01; // Start timer
PWM1->ENABLE = 0x02; // Start PWM1M1
PWM1->SYNC = 0x00000004; // Global Synchronization (supposedly) `
`PWM1->_1_CTL = 0x00; // Stop counter for proper activation
PWM1->_1_GENB = 0x0000008C; // M1PWM3 output set when reloaded, clear when matched with PWMCMPA (8250)
PWM1->_1_LOAD = 9612; // Set load value for 1kHz 16MHz/16000)
PWM1->_1_CMPA = 8250; // Set duty cycle to %
PWM1->_1_CTL = 0x01; // Start timer
PWM1->ENABLE = 0x08; // Start PWM1M3
PWM1->SYNC = 0x00000004; // Global Synchronization (supposedly)`
`PWM1->_2_CTL = 0x00; // Stop counter for proper activation
PWM1->_2_GENB = 0x0000008C; // M1PWM5 output set when reloaded, clear when matched with PWMCMPA (8250)
PWM1->_2_LOAD = 9612; // Set load value for 1kHz 16MHz/16000)
PWM1->_2_CMPA = 8250; // Set duty cycle to %
PWM1->_2_CTL = 0x01; // Start timer
PWM1->ENABLE = 0x20; // Start PWM1M5
PWM1->SYNC = 0x00000004; // Global Synchronization (supposedly)`
`PWM1->_3_CTL = 0x00; // Stop counter for proper activation
PWM1->_3_GENB = 0x0000008C; // M1PWM7 output set when reloaded, clear when matched with PWMCMPA (8250)
PWM1->_3_LOAD = 9612; // Set load value for 1kHz (16MHz/16000)
PWM1->_3_CMPA = 8250; // Set duty cycle to 20%
PWM1->_3_CTL = 0x01; // Start timer
PWM1->ENABLE = 0x80; // Start PWM1M7
PWM1->SYNC = 0x00000004; // Global Synchronization (supposedly)`
`//PWM0->_0_CTL = 0x00; // Stop counter for proper activation
//PWM0->_0_GENB = 0x0000008C;
//PWM0->_0_LOAD = 9612; // Set load value for 1kHz 16MHz/16000)
//PWM0->_0_CMPA = 8250; // Set duty cycle to %
//PWM0->_0_CTL = 0x01; // Start timer
//PWM0->ENABLE = 0x02; // Start PWM1
//PWM0->SYNC = 0x00000004; // Global Synchronization (supposedly)`
`while (1)
{
Bell_01();
delayMs(100);
Bell_02();
delayMs(100);
Bell_03();
delayMs(100);
Bell_04();
delayMs(100);
}`
`}`
`void delayMs(int n) // Delay n milliseconds (16 MHz CPU clock)
{
int i, j;
for(i = 0 ; i < n; i++)
for(j = 0; j < 3180; j++)
{} // Do nothing for 1 ms
}`
`void Bell_01()
{
PWM1->_0_CMPA = 3500;
delayMs(50);
PWM1->_0_CMPA = 8250;
}`
`void Bell_02()
{
PWM1->_1_CMPA = 3500;
delayMs(50);
PWM1->_1_CMPA = 8250;
}`
`void Bell_03()
{
PWM1->_2_CMPA = 3500;
delayMs(50);
PWM1->_2_CMPA = 8250;
}`
`void Bell_04()
{
PWM1->_3_CMPA = 3500;
delayMs(50);
PWM1->_3_CMPA = 8250;
}`
Upvotes: 0
Views: 5