Mike - K8LH
Well-Known Member
Would you be interested in a Charlieplexed display with a hi-speed hi-rez BAM driver for 12F1840 or 16F18313?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Would you be interested in a Charlieplexed display with a hi-speed hi-rez BAM driver for 12F1840 or 16F18313?
View attachment 125287
Does it have a Port B if it only has 8 pins? I looked through the datasheet and didn't see a reference to Port B.
So Charlie-Plexing is the only way to add more LEDs? So, if I do that, I need to change the pins to in & out, which will add to the complexity of the programming. I told myself that I needed to study and read more...lol I need to quite goofing around and get to reading.
It's an 8 pin PIC, it only has RA0 - RA5.To use RA7 instead of RA3.
You are correct, RA3 is input only, having just tried it..
The datasheet starts out saying "6 I/O" - but it also refers to a footnote saying one is input only; I missed that.
I thought the config bit to disable external reset allowed full function - but I never tested it.
ledout = calc_pwm(LED_P0);
if(ledout == 1) {
outimg = outimg | 0x01;
}
else {
outimg = outimg & ~0x01;
}
// The same sequence for the next:
ledout = calc_pwm(LED_P1);
if(ledout == 1) {
outimg = outimg | 0x02;
}
else {
outimg = outimg & ~0x02;
}
// And again for each pin, passing the phase offset
// then setting the appropriate pin
ledout = calc_pwm(LED_P2);
if(ledout == 1) {
outimg = outimg | 0x04;
}
else {
outimg = outimg & ~0x04;
}
ledout = calc_pwm(LED_P3);
if(ledout == 1) {
outimg = outimg | 0x10;
}
else {
outimg = outimg & ~0x10;
}
ledout = calc_pwm(LED_P4);
if(ledout == 1) {
outimg = outimg | 0x20;
}
else {
outimg = outimg & ~0x20;
}
The interrupt driven BAM (Bit Angle Modulation) driver takes care of that for you. A small program reads records from an array (table) that contains brightness levels for each LED (0..31) plus a duration value. The BAM driver provides 32 linear brightness levels spanning a 256 step (250-uS steps) period for each LED and the entire 12 LED display is refreshed every 768-usecs (1302-Hz refresh rate)... This is a high performance method that's probably over-kill for your application...So Charlie-Plexing is the only way to add more LEDs? So, if I do that, I need to change the pins to in & out, which will add to the complexity of the programming. I told myself that I needed to study and read more...lol I need to quite goofing around and get to reading.
/*
* Rotating strobe simulator
*
* (C) 2020 Robert Jenkins
* Released under GNU GPL licence.
*
* This version made to be buildable
* using MPLAB X and the free XC8 compiler
*
*/
// 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 = OFF // 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>
// Pin configuration used with 16F18313
//
// 01 = VDD +5V
// 02 = RA5 LED 4
// 03 = RA4 LED 3
// 04 = RA3 / MCLR VPP for pprogramming
// 05 = RA2 LED 2
// 06 = RA1 LED 1
// 07 = RA0 LED 0
// 08 = VSS 0V
uint8_t pwmreg; //address configuration
uint8_t stepcount; //
uint8_t substep; // PWM cycles per step increment; speed setting
uint8_t outimg;
// Master, coarse speed setting value; larger is slower.
#define SUBSTEP_MAX 9
// 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
// With a 32MHz clock, 120 steps can still run ludicrously fast.
#define STEP_MAX 120
// Output phase, equal offsets through the "step" cycle.
// eg. With 120 step table, 0-40-80 for three outputs,
// 0-30-60-90 for four, 0-24-48-72-96 for five,
// 0-20-40-60-80-100 for six and so on.
//
#define LED_P0 0
#define LED_P1 24
#define LED_P2 48
#define LED_P3 72
#define LED_P4 96
//#define LED_P5
//
// The association between LED numbers and output
// pins is created in each pin routine, further down.
//
// Look-up table for brightness sequence for one light
// Brightness range is from 0 (completely off) to PWM_MAX (always on)
uint8_t ltable1[STEP_MAX] =
// 120 Steps, 3' per step.
// This one is for a very narrow beam angle lens front, around 10'
// 180' to -138'
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// -135' to -93'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// -90' to -48'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// -45' to -3'
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 4, 10, 24,
// Zero degrees to +42'
32, 24, 10, 4, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
// +45' to +87'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// +90' to +132'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// +135' to +177'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t ltable2[STEP_MAX] =
// 120 Steps, 3' per step.
// This one is for a wide beam & open reflector, around 30'
// 180' to -138'
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// -135' to -93'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// -90' to -48'
0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6,
// -45' to -3'
6, 7, 8, 9, 10, 11, 12, 15, 24, 26, 28, 29, 30, 31, 31,
// Zero degrees to +42'
32, 31, 31, 30, 29, 28, 26, 24, 15, 12, 11, 10, 9, 8, 7,
// +45' to +87'
6, 6, 5, 5, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1,
// +90' to +132'
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// +135' to +177'
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 > device pin bit at a time.
// Get the on/off state and set the appropriate output pin
// Outputs high for on; LED & resistor to 0V.
// The | / ~ number used is the hex value that puts a single bit on the appropriate output pin.
ledout = calc_pwm(LED_P0);
if(ledout == 1) {
outimg = outimg & ~0x01;
}
else {
outimg = outimg | 0x01;
}
// The same sequence for the next:
ledout = calc_pwm(LED_P1);
if(ledout == 1) {
outimg = outimg & ~0x02;
}
else {
outimg = outimg | 0x02;
}
// And again for each pin, passing the phase offset
// then setting the appropriate pin image in outimg
ledout = calc_pwm(LED_P2);
if(ledout == 1) {
outimg = outimg & ~0x04;
}
else {
outimg = outimg | 0x04;
}
ledout = calc_pwm(LED_P3);
if(ledout == 1) {
outimg = outimg & ~0x10;
}
else {
outimg = outimg | 0x10;
}
ledout = calc_pwm(LED_P4);
if(ledout == 1) {
outimg = outimg & ~0x20;
}
else {
outimg = outimg | 0x20;
}
/*
ledout = calc_pwm(LED_P5);
if(ledout == 1) {
outimg = outimg & ~0x20;
}
else {
outimg = outimg | 0x20;
}
*/
//
// Set all the pins in the port simultaneously
// by copying outimg to the port register.
//
PORTA = outimg;
//
// Or use PORTA = ~outimg;
// and comment out the above line,
// if you need pins high for light on
//
// PORTA = ~outimg;
//
// For larger devices with two or more ports, use two or
// more outimg - eg. outimga & outimgb
// processing each bit as abovem then copying both to the
// respective ports.
//
// Add a few microseconds delay here to give a sine speed adjustment.
// Change the
// 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 > SUBSTEP_MAX) {
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
// Use ltable1 or ltable2 as appropriate.
y = ltable2[x];
// Compare to present PWM value to determine on or off
if(y > pwmreg) {
return 1;
}
return 0;
}
void interrupt ISR() // Michael McLaren, K8LH
{ static unsigned char step = 0;
static unsigned char toggle[64] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
PORTB ^= toggle[step]; // update channel outputs
toggle[step] = 0; // clear element for next period
step++; //
if(step == 64) // if end-of-period
{ step = 0; //
toggle[led[0]] |= 1; // insert LED 0 toggle bit
toggle[led[1]] |= 2; // insert LED 1 toggle bit
toggle[led[2]] |= 4; // insert LED 2 toggle bit
toggle[led[3]] |= 8; // insert LED 3 toggle bit
toggle[led[4]] |= 16; // insert LED 4 toggle bit
toggle[led[5]] |= 32; // insert LED 5 toggle bit
toggle[led[6]] |= 64; // insert LED 6 toggle bit
toggle[led[7]] |=128; // insert LED 7 toggle bit
toggle[0] ^= ~PORTB; // initialize first toggle element
} //
TMR0 = 210; // adjusts step size & frame rate
TMR0IF = 0; //
} //
So, I can leave them commented the way they are as long as I select which one to use (ltable1 or ltable2) for "y" at the bottom? I added another table to see the differences.
So if I wanted to change the speed, do I change a combination or just one specific area.