C program for PWM

Status
Not open for further replies.

pratiksurana

New Member
Hey everyone,

I am using MSP430F169 microcontroller for my robot, i have made the motor driver circuit but i am stuck with programmng coz i havent done programmng be4. Can any1 help me.

Thank you
 
Actually, i am a new user. Thatz why i dont have a clue how to do it. So i was wondering if you could tell me how to start n go about the program.

Cheers
 
You can use Timer A3 and Timer B7 to create PWM signals in MSP430F169.. Also do a software pwm with small part of code. You just need to determine your work frequency and duty cycle and on/off the output pin in a loop for soft pwm.
 
Code

Code is what i am not aware of. I dnt knw how 2 strt, coz i hve never done programming before. I want to learn bt dnt knw whr 2 strt!!!

Any help???
 
You were using English earlier. Did you forget, or do you think SMS-type is cute? We don't do cute here.
 
hello

do you have a cd that comes with a microcontroller, if you have you can get programs from their if not, copy this to whatever software, i don't guarantee that this will work, if you use a lpc2119 i guarantee. you should try to change to pin address if it doesn't works.

/*****************************************************/
/* Examples Program For "ET-ARM STAMP LPC2119" Board */
/* Target MCU : Philips ARM7-LPC2119 */
/* : X-TAL : 19.6608 MHz */
/* : Run Speed 58.9824MHz (With PLL) */
/* Compiler : Keil ARM V2.32a */
/* Create By : Eakachai Makarn (WWW.ETT.CO.TH) */
/* Last Update : 1/September/2005 */
/* Function : Example PWM2(P0.7) & ADC0(P0.27) */
/*****************************************************/
// Used GPIO0.27 = AIN0 (ADC)
// GPIO0.7 = PWM2
// Read ADC0 ----> Update PWM2
// PWM2 Mode = Double Edge
// = Match1 --> Set Output PWM(Low Pulse = Match1...Match0)
// = Match2 --> Reset Output PWM(High Pulse = Match2...Match0)
// Period = 1024 Cycle of 29.4912 MHz(28.80KHz/34.722uS Period)
// High Pulse = 0..1023(10-Bit ADC Result)
// Low Pulse = 1024-(ADC Result[0..1023])

#include <LPC21xx.H> // LPC2119 MPU Register

/* pototype section */
unsigned int val; // ADC Result (HEX)

// Main Program Start Here //
int main(void)
{
// Initial ADC0 (GPIO-0.27) By Set PINSEL1[23:22=01]
PINSEL1 &= 0xFF7FFFFF; // Select ADC0 Pin Connect P0.27
PINSEL1 != 0x00400000;

// Initial PWM2 (GPIO-0.7) By Set PINSEL0[15:14=10]
PINSEL0 &= 0xFFFF3FFF; // Select PWM2 Pin Connect P0.7
PINSEL0 |= 0x00008000;

// Initial ADC0 (ADCR=0x01210601)
ADCR &= 0x00000000; // Clear All Bit Control
ADCR |= 0x00000001; // Select ADC = AIN0
ADCR |= 0x00000600; // ADC Clock = VBP(PCLK) / 7
ADCR |= 0x00010000; // Busrt = 1 = Conversion Continue
ADCR &= 0xFFF1FFFF; // CLKS = 000 = 10Bit : 11 Cycle Clock Conversion
ADCR |= 0x00200000; // PDN = 1 = Active ADC Module
ADCR &= 0xFF3FFFFF; // TEST[1:0] = 00 = Normal Mode
ADCR |= 0x01000000; // START = 001 = Start Conversion Now
ADCR &= 0xF7FFFFFF; // EDGE = 0 = Conversion on Falling Edge

// Initial PWM2 = 28.8KHz (29.4912/1024=28.800 KHz)
// Period = 34.722 uS
PWMPR = 0; // 35uS Period -> 28.80KHz

//Initial PWM2 = Double Edge PWM
// Set Output By Match-1
// Reset Output By Match-2
PWMPCR |= 0x00000004; // PWMSEL2 = "1" = Double Edge Control
PWMPCR |= 0x00000400; // PWMENA2 = "1" = Enable PWM2
PWMMCR = 0x00000002; // On Match0 = Reset Counter
PWMMR0 = 0x00000400; // Set PWM2 Rate(29.4912MHz/1024)
PWMMR1 = 0x00000000; // Set Default PWM2 High Pulse(0...PWMMR2 Cycle)
PWMMR2 = 0x00000200; // Set Default PWM2 Low Pulse (512...1024 Cycle)
PWMLER = 0x00000007; // Enable Shadow Latch For Match 0,1,2
PWMTCR = 0x00000002; // Reset Counter and Prescaler
PWMTCR = 0x00000009; // Enable Counter and PWM + Release Counter From Reset

// Loop Generate PWM2 Drive DC Motor //
while(1) // Loop Continue
{
do // Loop Read ADC0
{
val = ADDR; // Read A/D 10-Bit Data Register
}
while ((val & 0x80000000) == 0); // Wait ADC Conversion Complete
val = (val >> 6) & 0x03FF; // Shift ADC Result to Integer

/* Update High Pulse = 10-Bit ADC Result(0..1023) */
PWMMR2 = 0x00000000+val; // Update PWM2(High Period = 0..1023 Cycle)
PWMLER = 0x00000004; // Enable PWM Match2 Latch
}
}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…