In this post we are going to extend our previous timer based 7 segment multiplexing code. We are going to add a UART in that code and will update the 9 digit 7 segment multiplexing. We will send the digits to be displayed on 7 segment which we send from the serial port using uart module and with 9600 baud rate. You can change the baudrate whatever you want.
Here is the complete working code to display uart received values on 7 segment display.
#include <16F887.h>
#device *= 16 ADC=10
#fuses NOWDT, HS, PROTECT, CPD, NOWRT, BROWNOUT, NODEBUG, NOLVP, PUT
#use delay(clock=16000000)
#define OUT_SEG_CODE(CODE) output_b(CODE)
#use rs232(uart1, baud = 9600)
#define NUM_DIGITS 9
unsigned char seg_codes[] = {0x03, 0x9F, 0x25, 0x0D, 0x99, 0x49, 0x41, 0x1F, 0x01, 0x09, 0xFF, 0xE5, 0x91};
int seg_commons[] = {PIN_C0, PIN_C1, PIN_C2, PIN_C3, PIN_D0, PIN_D1, PIN_D2, PIN_D3, PIN_C4};
unsigned char allDigits[] = {11, 2, 3, 11, 5, 6, 12, 8, 9};
//--------Settings -----------------
#define SW_SET PIN_A0
#define SW_UP PIN_A1
#define SW_DOWN PIN_A2
// State variables
int currentState = 0;
signed int8 settings[]={0,40,45,73};
volatile char received_data;
volatile int data_count = 0;
volatile char data_buffer[4];
//====================================================================
// ISRs
//====================================================================
#INT_TIMER0
void timer0_isr(void)
{
static int8 ms_counter=0,cur_segment=0;
if(cur_segment==0)output_high(seg_commons[NUM_DIGITS-1]);
else output_high(seg_commons[cur_segment-1]);
OUT_SEG_CODE(seg_codes[allDigits[cur_segment]]);
output_low(seg_commons[cur_segment]);
cur_segment++;
if(cur_segment>NUM_DIGITS)cur_segment=0;
clear_interrupt(INT_TIMER0);
set_timer0(999);
}
//====================================================================
char i =0,count=0;
void main(){
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8); // Prescaler of 8
set_timer0(999); // Set Timer0 reload value to 999 for 500 µs interval
clear_interrupt(INT_TIMER0);
enable_interrupts(INT_TIMER0);
//enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
putc(13); // Go to first column
printf("Hello world!");
while(TRUE){
int8 c =0;
if(kbhit()){
i = getc(); // UART read
putc(i);
if(i=='['){
count=0;
}else{
if(i=='H')i=12+48;
if(i=='c')i=11+48;
allDigits[count]=i - 48;
count++;
}
}
}
}
Code language: C++ (cpp)