Hello, im trying to use a PWM, i've read the CookBook so I did the following:
#include "LPC845.h"
void setDuty(uint8_t duty);
int main(){
//SW, SCTimer,GPIO1,ICON
SYSCON->SYSAHBCLKCTRL0 |= (1<<7) | (1<<8)|(1<<18)|(1<<20);
SCT->CONFIG |= (1<<0)|(1<<17);
SCT->MATCH[0]=FREQ_CLOCK_MCU/1000;
SCT->MATCHREL[0]=FREQ_CLOCK_MCU/1000;
SCT->EV[0].STATE=0xFFFFFFFF;
SCT->EV[0].CTRL= 0 | (1<<12);
SCT->OUTPUT=0xC;
SCT->RES=0b01010101;
SCT->EVEN=1;
SCT->EV[1].STATE=0xFFFFFFFF;
SCT->EV[1].CTRL = 1 | (1<<12);
SCT->OUT[0].SET = 1;
SCT->OUT[0].CLR = (1<<1);
SCT->MATCH[1] = 1;
SCT->MATCHREL[1] = 1;
NVIC->ISER[0] = (1<<9);
SWM0->PINASSIGN.PINASSIGN8=1*32+1; //BLUE LED
SCT->CTRL &= ~(1 << 2);
setDuty(20);
while(1){}
return 0;
}
void setDuty(uint8_t duty){
SCT->MATCHREL[1] = FREQ_CLOCK_MCU/1000*duty/100;
}
However this doesn't work, i've spent like 5 hours thinking about this and can't find why can anyone help on this? I am not using any SDK on purpose. This does turn on the blue led on the board (I turn off all LEDs first but didn't paste it here)
Hi,
For your code, pls refer to the modification.
#include "LPC845.h"
void setDuty(uint8_t duty);
int main(){
//SW, SCTimer,GPIO1,ICON
SYSCON->SYSAHBCLKCTRL0 |= (1<<7) | (1<<8)|(1<<18)|(1<<20);
SYSCON->SCTCLKsel=1; //USE MAIN CLOCK AS sct CLOCK
SYSCON->SCTCLKDIV=1; //enable the SCT clock
SCT->CONFIG |= (1<<0)|(1<<17);
SCT->MATCH[0]=FREQ_CLOCK_MCU/1000;
SCT->MATCHREL[0]=FREQ_CLOCK_MCU/1000;
SCT->EV[0].STATE=0xFFFFFFFF;
SCT->EV[0].CTRL= 0 | (1<<12);
SCT->OUTPUT=0xC;
SCT->RES=0b01010101;
SCT->EVEN=1;
SCT->EV[1].STATE=0xFFFFFFFF;
SCT->EV[1].CTRL = 1 | (1<<12);
SCT->OUT[0].SET = 1;
SCT->OUT[0].CLR = (1<<1);
SCT->MATCH[1] = FREQ_CLOCK_MCU/(2*1000);
SCT->MATCHREL[1] = FREQ_CLOCK_MCU/(2*1000);
NVIC->ISER[0] = (1<<9);
SWM0->PINASSIGN.PINASSIGN8=1*32+1; //BLUE LED
SCT->CTRL &= ~(1 << 2);
setDuty(20);
while(1){}
return 0;
}
void setDuty(uint8_t duty){
SCT->MATCHREL[1] = FREQ_CLOCK_MCU/(1000*2);
}
Pls try to use the above code.
I suppose that you'd better connect a scope to PIO1_1 and check if the PWM signal appears. You can not see the LED toggles, because the PWM is 1KHz signal, it is too fast to sere the LED toggles, the LED always turns on.
If you have not scope and have to use LED to see PWM, you can output low frequency PWM signal for example 1Hz and check if the LED toggles with the following configuration.
SCT->MATCH[0]=SCT->MATCHREL[0]=FREQ_CLOCK_MCU/1;
SCT->MATCH[1]=SCT->MATCHREL[1]=FREQ_CLOCK_MCU/2;
If you still have issue, pls check the SCT->COUNT in tools debugger and if the counter changes it's value.
Hope it is helpful
BR
XiangJun Rong
Well, tried what you sent (on 1Hz)and it doesn't flicker. How could I check SCT->COUNT?
Thanks, i'll try that. I know I can't see the flicker with 1kZ PWM, but with duty at 20% the brightness should be a lot lower, which it isn't.
Hi,
I will have a test next week. for the following code, the SCT0 counter can count tick, but the red led can not toggle, I have not a scope on hand, I will test with a scope next week.
BR
XiangJun Rong
#define FREQ_CLOCK_MCU 18000000
void SCTimerInit(void)
{
//SW, SCTimer,GPIO1,ICON
SYSCON->SYSAHBCLKCTRL0 |= (1<<7) | (1<<8)|(1<<18)|(1<<20);
SYSCON->SCTCLKSEL=1; //USE MAIN CLOCK AS sct CLOCK
SYSCON->SCTCLKDIV=1; //enable the SCT clock
SCT0->CONFIG |= (1<<0)|(1<<17);
SCT0->MATCH[0]=(uint32_t)(FREQ_CLOCK_MCU/1);
SCT0->MATCHREL[0]=(uint32_t)(FREQ_CLOCK_MCU/1);
SCT0->EV[0].STATE=0xFFFFFFFF;
SCT0->EV[0].CTRL= 0 | (1<<12);
SCT0->OUTPUT=0xC;
SCT0->RES=0b01010101;
SCT0->EVEN=1;
SCT0->EV[1].STATE=0xFFFFFFFF;
SCT0->EV[1].CTRL = 1 | (1<<12);
SCT0->OUT[0].SET = 1<<1;
SCT0->OUT[0].CLR = 1;
SCT0->MATCH[1] = (uint32_t)(FREQ_CLOCK_MCU/(2*1));
SCT0->MATCHREL[1] = (uint32_t)(FREQ_CLOCK_MCU/(2*1));
//NVIC->ISER[0] = (1<<9);
//SWM0->PINASSIGN.PINASSIGN8=1*32+15; //BLUE LED
SWM0->PINASSIGN.PINASSIGN8=0; //P0_0 green led
SCT0->CTRL &= ~(1 << 2);
setDuty(20);
while(1){}
}
Managed to make it work with the following code:
SYSCON->SYSAHBCLKCTRL0 |= (1<<7) | (1<<8)| (1<<18) | (1<<20);
SWM0->PINASSIGN.PINASSIGN7 &= ((32*1+2 << 24) | 0x00FFFFFF);; //RED LED
SCT->CONFIG |= (1<<0)|(1<<17);
SCT->MATCH[0]=FREQ_CLOCK_MCU/(1000); //1KHz on unidrectional mode
SCT->MATCHREL[0]=FREQ_CLOCK_MCU/(1000);
SCT->EV[0].STATE=1; //any number except 0
SCT->EV[0].CTRL= 0 | (1<<12);
SCT->OUTPUT=0xC;
SCT->RES=0b01010101;
SCT->EVEN=1;
SCT->EV[1].STATE=1;//any number except 0
SCT->EV[1].CTRL = 1 | (1<<12);
SCT->OUT[0].SET = 1;
SCT->OUT[0].CLR = (1<<1);
SCT->MATCH[1] = 0;
SCT->MATCHREL[1] = 0;
SCT->CTRL &= ~(1 << 2);
Removed interruption and changed PINASSIGN on SWM
Thanks, i'll tell you if I can fix it in the weekend