#include <16F18313.h>
#zero_ram
#DEVICE PIC16F18313 ICD=1
#USE DELAY(internal=4MHz)
#USE FAST_IO (A)
#include <stdint.h>
// Do all pin selects
// 01 = VDD +5V
// 02 = RA5
// 03 = RA4
// 04 = RA3 / MCLR Debug VPP
// 05 = RA2
// 06 = RA1 / ICSP CLK
// 07 = RA0 / ICSP DAT
// 08 = VSS 0V
//#include <stdio.h>
//#include <stdlib.h>
uint8_t pwmreg; //address configuration
uint8_t stepcount; //
uint8_t substep; // PWM cycles per step increment; speed setting
uint8_t outimg;
// PWM range 0-x; a (power of 2) - 1 value
// Using 0 - 31
#define PWM_MAX 0x1f
// Brightness steps for a full rotation sequence of one light;
// eg. 4 or 8 times the number of different-brightness lights, -1
#define STEP_MAX 64
// Output phase, equal offsets through the "step" cycle.
#define LED_P0 0
#define LED_P1 16
#define LED_P2 32
#define LED_P3 48
// Look-up table for brightness sequence for one light
// Brightness range 0 = PWM_MAX
uint8_t ltable[STEP_MAX] =
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 5, 7, 8, 9, 10, 12, 15, 17, 20, 22, 25, 28, 32,
32, 28, 25, 22, 20, 17, 15, 12, 10, 9, 8, 7, 5, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Function definition
uint8_t calc_pwm(uint8_t);
/*
*
*/
int main(int argc, char** argv) {
int8_t ledout;
outimg = 0;
pwmreg = 0;
stepcount = 0;
substep = 0;
// I/O Pin directions:
// All out: TRIS = 0x0000 0000
set_tris_a(0x00);
// OSC = 32 MHz.
// Timer source = osc / 4; 8 MHz.
for(;;)
{
restart_wdt();
pwmreg = pwmreg + 1;
pwmreg = pwmreg & PWM_MAX;
// Do one LED
// Get the on/off state and set the appropriate output pin
// Outputs high for on; LED & resistor to 0V.
ledout = calc_pwm(LED_P0);
if(ledout == 1) {
outimg = outimg | 0x04;
}
else {
outimg = outimg & ~0x04;
}
// The same sequence for the next:
ledout = calc_pwm(LED_P1);
if(ledout == 1) {
outimg = outimg | 0x08;
}
else {
outimg = outimg & ~0x08;
}
// And again for each pin, passing the phase offset
// then setting the appropriate pin
ledout = calc_pwm(LED_P2);
if(ledout == 1) {
outimg = outimg | 0x10;
}
else {
outimg = outimg & ~0x10;
}
ledout = calc_pwm(LED_P3);
if(ledout == 1) {
outimg = outimg | 0x20;
}
else {
outimg = outimg & ~0x20;
}
output_a(outimg);
// At each 0 of the PWM reg, count the substep delay
// and if that overflows, move to the next sequence step.
if(pwmreg == 0) {
//
// Substep limit sets the overall cycle speed
substep = substep + 1;
if(substep > 4) {
substep = 0;
stepcount = stepcount + 1;
if(stepcount >= STEP_MAX)
{
stepcount = 0;
}
}
}
}
return (1);
}
uint8_t calc_pwm (uint8_t lp) {
uint8_t x, y;
// Work out the cycle stage for the lamp, then get the table brightness
// and compare to the PWM count to determine on/off
x = stepcount + lp;
// Wrap the result back to the start of the table;
// if it is beyond the end.
if(x >= STEP_MAX) {
x = x - STEP_MAX;
}
// Get the brightness value from the table
y = ltable[x];
// Compare to present PWM value to determine on or off
if(y > pwmreg) {
return 1;
}
return 0;
}
/*
* File: Rotating strobe simulator.c
* Author: Robert Jenkins
*/
// PIC16F18313 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FEXTOSC = OFF // FEXTOSC External Oscillator mode Selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with 2x PLL (32MHz))
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; I/O or oscillator function on OSC2)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config MCLRE = OFF // Master Clear Enable bit (MCLR/VPP pin function is digital input; MCLR internally disabled; Weak pull-up under control of port pin's WPU control bit.)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config WDTE = OFF // Watchdog Timer Enable bits (WDT enabled, SWDTEN is ignored)
#pragma config LPBOREN = OFF // Low-power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bits (Brown-out Reset enabled, SBOREN bit ignored)
#pragma config BORV = LOW // Brown-out Reset Voltage selection bit (Brown-out voltage (Vbor) set to 2.45V)
#pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (The PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a Reset)
#pragma config DEBUG = ON // Debugger enable bit (Background debugger enabled)
// CONFIG3
#pragma config WRT = OFF // User NVM self-write protection bits (Write protection off)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (HV on MCLR/VPP must be used for programming.)
// CONFIG4
#pragma config CP = OFF // User NVM Program Memory Code Protection bit (User NVM code protection disabled)
#pragma config CPD = OFF // Data NVM Memory Code Protection bit (Data NVM code protection disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdint.h>
// Do all pin selects
// 01 = VDD +5V
// 02 = RA5
// 03 = RA4
// 04 = RA3 / MCLR Debug VPP
// 05 = RA2
// 06 = RA1 / ICSP CLK
// 07 = RA0 / ICSP DAT
// 08 = VSS 0V
uint8_t pwmreg; //address configuration
uint8_t stepcount; //
uint8_t substep; // PWM cycles per step increment; speed setting
uint8_t outimg;
// PWM range 0-x; a (power of 2) - 1 value
// Using 0 - 31
#define PWM_MAX 0x1f
// Brightness steps for a full rotation sequence of one light;
// eg. 4 or 8 times the number of different-brightness lights, -1
#define STEP_MAX 64
// Output phase, equal offsets through the "step" cycle.
#define LED_P0 0
#define LED_P1 16
#define LED_P2 32
#define LED_P3 48
// Look-up table for brightness sequence for one light
// Brightness range 0 = PWM_MAX
uint8_t ltable[STEP_MAX] =
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 5, 7, 8, 9, 10, 12, 15, 17, 20, 22, 25, 28, 32,
32, 28, 25, 22, 20, 17, 15, 12, 10, 9, 8, 7, 5, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Function definition
uint8_t calc_pwm(uint8_t);
/*
*
*/
int main(int argc, char** argv) {
int8_t ledout;
outimg = 0;
pwmreg = 0;
stepcount = 0;
substep = 0;
// I/O Pin directions:
// All out: TRIS = 0x0000 0000
TRISA = 0x00;
// OSC = 32 MHz.
// Timer source = osc / 4; 8 MHz.
for(;;)
{
CLRWDT();
pwmreg = pwmreg + 1;
pwmreg = pwmreg & PWM_MAX;
// Do one LED
// Get the on/off state and set the appropriate output pin
// Outputs high for on; LED & resistor to 0V.
ledout = calc_pwm(LED_P0);
if(ledout == 1) {
outimg = outimg | 0x04;
}
else {
outimg = outimg & ~0x04;
}
// The same sequence for the next:
ledout = calc_pwm(LED_P1);
if(ledout == 1) {
outimg = outimg | 0x08;
}
else {
outimg = outimg & ~0x08;
}
// And again for each pin, passing the phase offset
// then setting the appropriate pin
ledout = calc_pwm(LED_P2);
if(ledout == 1) {
outimg = outimg | 0x10;
}
else {
outimg = outimg & ~0x10;
}
ledout = calc_pwm(LED_P3);
if(ledout == 1) {
outimg = outimg | 0x20;
}
else {
outimg = outimg & ~0x20;
}
PORTA = outimg;
// At each 0 of the PWM reg, count the substep delay
// and if that overflows, move to the next sequence step.
if(pwmreg == 0) {
//
// Substep limit sets the overall cycle speed
substep = substep + 1;
if(substep > 18) {
substep = 0;
stepcount = stepcount + 1;
if(stepcount >= STEP_MAX)
{
stepcount = 0;
}
}
}
}
return (1);
}
uint8_t calc_pwm (uint8_t lp) {
uint8_t x, y;
// Work out the cycle stage for the lamp, then get the table brightness
// and compare to the PWM count to determine on/off
x = stepcount + lp;
// Wrap the result back to the start of the table;
// if it is beyond the end.
if(x >= STEP_MAX) {
x = x - STEP_MAX;
}
// Get the brightness value from the table
y = ltable[x];
// Compare to present PWM value to determine on or off
if(y > pwmreg) {
return 1;
}
return 0;
}
Alright. I was finally able to write the code to the PIC 16F18313. It scared me initially as it took longer than expected. I was happy to see some LEDs lit, but it is performing like your video where only three (RA2, RA4 & RA5) are fading and none of the others are lit at all.
Yes, I did set the debug to off. Should I use a different uP?
// The same sequence for the next:
ledout = calc_pwm(LED_P1);
if(ledout == 1) {
outimg = outimg | 0x08;
}
else {
outimg = outimg & ~0x08;
}
// The same sequence for the next:
ledout = calc_pwm(LED_P1);
if(ledout == 1) {
outimg = outimg | 0x80;
}
else {
outimg = outimg & ~0x80;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?