Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pwm example #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.pwm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
README.pwm

this example demonstrates how to setup the LPC810 SCT function to generate a PWM signal.
The led (pio0_2) is used to output the PWM signal visually.
The input pin pio0_1 (ISP-button on LPC800 minikit) allows to change the duty cycle.


148 changes: 148 additions & 0 deletions src/pwm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**************************************************************************/
/*!
@file pwm.c

@section LICENSE

Software License Agreement (BSD License)

Copyright (c) 2014, Mat Kattanek
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**************************************************************************/

/*
@info
setup SCT as 32bit timer.
generate PWM depending on match0 (period) and match1 (duty cycle) register.
using pio0_2 to flash led.
*/

#include <stdio.h>
#include "LPC8xx.h"
#include "gpio.h"
#include "mrt.h"
#include "uart.h"

extern void configurePins(); // main.c

#define ISP_BUTTON (1)

#define MATCH_PERIOD (SystemCoreClock/2) // 2hz
#define MATCH_DUTY (SystemCoreClock/20) // 10% duty cycle

void
sct_fsm_init (void)
{
LPC_SCT->CONFIG = (LPC_SCT->CONFIG & ~0x00060001) | 0x00000001; // UNIFIED 32bit

//--- MATCH/CAPTURE registers
LPC_SCT->REGMODE_L = 0x0000; // L: 2x MATCH, 0x CAPTURE, 3 unused
LPC_SCT->MATCH[0].U = MATCH_PERIOD; // MATCH0
LPC_SCT->MATCHREL[0].U = MATCH_PERIOD;
LPC_SCT->MATCH[1].U = MATCH_DUTY; // MATCH1
LPC_SCT->MATCHREL[1].U = MATCH_DUTY;

//--- OUTPUT registers
LPC_SCT->OUT[0].SET = 0x00000001; // Output_pin_0 set on event0
LPC_SCT->OUT[0].CLR = 0x00000002; // clear on event1
LPC_SCT->OUT[1].SET = 0; // Unused outputs must not be affected by any event
LPC_SCT->OUT[1].CLR = 0;
LPC_SCT->OUT[2].SET = 0;
LPC_SCT->OUT[2].CLR = 0;
LPC_SCT->OUT[3].SET = 0;
LPC_SCT->OUT[3].CLR = 0;

//--- EVENT registers
LPC_SCT->EVENT[0].CTRL = 0x00005000; // L: --> state L_ENTRY
LPC_SCT->EVENT[0].STATE = 0x00000001;
LPC_SCT->EVENT[1].CTRL = 0x00005001; // L: --> state L_ENTRY
LPC_SCT->EVENT[1].STATE = 0x00000001;
LPC_SCT->EVENT[2].STATE = 0; // Unused events must not have any effect
LPC_SCT->EVENT[3].STATE = 0;
LPC_SCT->EVENT[4].STATE = 0;
LPC_SCT->EVENT[5].STATE = 0;

//--- STATE registers
LPC_SCT->STATE_L = 0;
LPC_SCT->STATE_H = 0;

//--- CORE registers
LPC_SCT->START_L = 0x0000;
LPC_SCT->STOP_L = 0x0000;
LPC_SCT->HALT_L = 0x0000;
LPC_SCT->LIMIT_L = 0x0001;
LPC_SCT->START_H = 0x0000;
LPC_SCT->STOP_H = 0x0000;
LPC_SCT->HALT_H = 0x0000;
LPC_SCT->LIMIT_H = 0x0000;
LPC_SCT->EVEN = 0x00000000;
}

#if 0

//
// To activate the pwm main() function you need to rename
// the main() in main.c
//

int
main(void)
{
gpioInit(); // Initialize the GPIO block

uart0Init(115200); // Initialize uart for printf output

mrtInit(__SYSTEM_CLOCK/1000); // Configure the multi-rate timer for 1ms ticks

configurePins(); // Configure the switch matrix (setup pins UART and GPIO)

LPC_SWM->PINASSIGN6 = 0x02FFFFFF; // SCT OUTP_0 at P0.2 (led) on switch matrix
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 8); // enable the SCT clock

sct_fsm_init(); // Initialize SCT
LPC_SCT->CTRL_L &= ~(1<<2); // unhalt the SCT by clearing bit 2 in CTRL

LPC_GPIO_PORT->DIR0 &= ~(1 << ISP_BUTTON); // enable ISP button as input pin

while(1) {

mrtDelay(500); //take a snooze

if ( (LPC_GPIO_PORT->PIN0 & 0x02) == 0) { // pio0_1 ISP button pushed?
if (LPC_SCT->MATCHREL[1].U < MATCH_PERIOD ) {
LPC_SCT->MATCHREL[1].U += MATCH_DUTY; // increase duty cycle
}
else {
LPC_SCT->MATCHREL[1].U = 0; // rollover
}
}

// Send some text (redirected to UART)
printf("Hello, lpc810.pwm! dutycycle=%u \r\n", LPC_SCT->MATCHREL[1].U );
}
}
#endif
127 changes: 127 additions & 0 deletions tools/LPC810_pwm.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
:1000000000040010E5000000D5000000D900000049
:1000100000000000000000000000000000000000E0
:1000200000000000000000000000000000000000D0
:100030000000000000000000DD000000E100000002
:1000400041010000410100000000000041010000EA
:10005000410100004101000000000000000000001C
:100060004101000041010000890100004101000040
:1000700041010000410100000000000041010000BA
:100080000000000000000000000000000000000070
:100090000000000000000000000000000000000060
:1000A0004101000041010000410100004101000048
:1000B0004101000041010000410100004101000038
:1000C000B40700000000001004000000040000104D
:1000D0000400000000B5FEE700B5FEE700B5FEE74E
:1000E00000B5FEE770B5124B12480AE01D6859686A
:1000F0009C6800220C3302E05659043240C1A242EF
:10010000FAD38342F2D309E019685D680A1C083308
:1001100000E001C2561AAE4202D2FAE7064C0020B5
:10012000A342F1D300F06CFA00F0F0FAFEE7C0460B
:10013000C0000000CC000000D400000000B5FEE7C5
:1001400000B5FEE7074B40201A1DD16F0143D16770
:100150005968054A0A405A6059688022D2000A4309
:100160005A60704700800440FFFBFFFF044B054AC4
:1001700041211A60E0224942520099507047C0461E
:1001800000C000400400FFFF00B5054B0122D96804
:10019000114204D0DA60034B1A6801321A6000BDC4
:1001A000004000400400001010B50E4B8022191DC5
:1001B000CC6FD2001443CC675C6880218C435C60B8
:1001C0005C6821435960084B00211960074B80216E
:1001D0001860186809060143196001219960044BF1
:1001E0001A6010BD00800440040000100040004070
:1001F00000E100E000B5034B00221A601A68824259
:10020000FCD300BD0400001008B500F073FA08BD6F
:1002100008B5031E05D01A6811701A6801321A60F9
:1002200002E0081CFFF7F0FF08BDF7B5141C002220
:1002300001900F1C2026944201DC0CE00132B95CD5
:100240000029FBD1A24201DAA41A00E00C1C2026EE
:100250009A0700D530260025DA070CD4251C04E0C7
:100260000198311CFFF7D4FF013D002DF8DCE54378
:10027000ED172540641B7F1B03E00198FFF7C8FFC3
:100280000135795D0029F8D1271C04E00198311C63
:10029000FFF7BEFF013F002FF8DCE043C01704402A
:1002A0002819FEBDF0B589B00290171C0E9E0C1ED9
:1002B00009D105AB30221A705970321C191C0F9BE2
:1002C000FFF7B3FF3FE000220192002B06D00A2F78
:1002D00004D1944202DA01236442019305AB002267
:1002E000DA72109B6D463A3B1F3503930FE0201CDA
:1002F000391C00F00FFA092901DD039BC9183031C0
:10030000013D2970201C391C00F004FA041C002C4B
:10031000EDD1019C002C0FD0002E09D00F9A92072E
:1003200006D502982D21FFF773FF013E012403E05B
:10033000013D2D232B7000240298291C321C0F9B99
:10034000FFF773FF201809B0F0BD0000F0B589B0C9
:10035000061C0D1C0592002477E0252B6FD16978CF
:10036000002975D06A1C252968D000232D2901D1C8
:10037000AA1C0123151C02E00220013503432A7840
:10038000302AF9D000220A279C4602E07A43013540
:1003900012182978081C3038C3B2092BF6D96346E5
:1003A000732907D10599081D09680590002941D1D5
:1003B0002B493FE064290ED10599081D0968019376
:1003C0006123059000920293301C0A220123FFF75B
:1003D00069FF241838E0782907D10599081D059090
:1003E000096801930092612308E058290AD1059910
:1003F000081D059009680193009241230293301C67
:1004000010220BE075290BD10599081D096801938D
:100410006123059000920293301C0A220023D6E744
:10042000632911D10598011D0068059107A908707D
:1004300000204870301CFFF7F8FECAE7151C297829
:10044000301CFFF7E5FE013401352B78002B84D1F9
:10045000002E02D0336800221A70201C09B0F0BDB3
:10046000880700000FB407B504AA02CA0020019251
:10047000FFF76CFF03B008BC04B01847F8B52D4C6B
:100480002D4B22680126134033432360231C002593
:100490004C331D80294B271C186880224308FC37E9
:1004A00092007B601421A35000F034F981239B005B
:1004B000B860E050A023DB00E650214B0222E2505E
:1004C000A123DB00E5501F4BA022E550A223DB0057
:1004D000E5501D4BD201E550A323DB00E5501B4B3B
:1004E000E550C1239B00E250C0239B00E650184A10
:1004F000C3239B00E250C2239B00E650C4239B0011
:10050000E550C6239B00E550C8239B00E550CA2355
:100510009B00E550A31DDD870233DD87A582258280
:10052000A5812681E5826582E5816581F0342560BB
:10053000F8BDC04600400050FEFFF9FF000000106B
:10054000040500000C050000140500001C05000057
:1005500001500000F8B5FFF7F5FDE120400200F082
:1005600083F82048FFF720FEFFF700FE1E4A1F4BCE
:100570001F4E9A611F4B8022D96F52000A43DA67DF
:10058000FFF77CFFB38804229343B3808022A0232B
:100590001B0692019958022081439950FA2040008D
:1005A000FFF728FE8423A0219B010906CA580223D5
:1005B000134010D1104A8124A400106831590C4D09
:1005C0004208914206D2142100F0A4F82F59C7190D
:1005D0002F5100E02B5181239B00F1580748FFF772
:1005E00041FFDBE730750000FFFFFF0200C0004065
:1005F0000040005004800440000000108F070000FD
:1006000010B5154A1548D16F154B0143D16700222B
:100610001A6401225A64111C586C114A0842FBD01A
:10062000242191608E21890050588024A04350508D
:100630000120DC680A4901220442FAD0032008673D
:100640004A67596F1142FCD0054B02229A67054B4D
:1006500000221A6110BDC0460480044080000400DE
:100660000080044000000440F7B51E4C1E4F231CC0
:1006700094330122009008261A603B1DDE67231D7B
:10068000D96F8022D2010A43DA676368174DB343FA
:100690006360636800993343636004232B601448EC
:1006A00000F038F8231CF033FF22013828621A606A
:1006B0001868296A1B68013001930D4B01315843BA
:1006C000009BF434594300F025F8019AD343C01835
:1006D00082231B012060AB603E602B680122134324
:1006E0002B60F7BD0080044000E100E000400640C0
:1006F0007038390000B5044A042391681942FCD0CF
:10070000014BD86100BDC0460040064008B5FFF768
:1007100021FF08BD002931D00122D207030901E0E1
:10072000090112099942FBD9430801E04900520826
:100730009942FBD900E04908401A07D35241FAD345
:1007400001461046704749084018F7D29218FAD36C
:100750004018014610467047CB0F00D049420210A6
:1007600000D54042534008B5FFF7D4FF0CBC5210EF
:1007700000D34042002A00D549421847F4460020E1
:1007800000F001F860477047286E756C6C290048CE
:10079000656C6C6F2C206C70633831302E70776D07
:1007A0002120647574796379636C653D2575200D2E
:0407B0000A00FFFF3D
:0407B40080C3C90134
:04000003000000E514
:00000001FF