Quiero mostrar 4 datos diferentes en una pantalla multiplexada de siete segmentos. Puedo mostrar 2 datos durante un cierto intervalo, ahora quiero expandir esto a 4. Cómo hacerlo.
main(void) {
InitIO();
InitTimer2();
int i;
while(1) {
switch(i) {
case 1:
input();
break;
case 2:
output();
break;
}
if (0==my_timer) {
i=(1==i)?2:1;
my_timer = 30 * 50; // MUX_FREQUENCY must be < 1092 Hz
}
}
}
// código completo
/*
* File: main.c
* Author: EmbeddedBuzz
*
* Created on December 4, 2014, 7:24 PM
*/
#include <htc.h>
__CONFIG(FOSC_XT & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_ON);
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000
#define K 0.01f
//Display Configuration
unsigned short shifter, portb_index;
unsigned int digit, number;
unsigned short portb_array[4];
float i,adc,adc1;
float result,result1;
volatile int my_timer=0;
unsigned short mask(unsigned short num)
{
switch (num)
{
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
case 'I':return 0x06;
}
}
void interrupt ISR()
{
PORTC = 0; // Turn off all 7seg. displays;
PORTB =portb_array[portb_index]; // Bring appropriate value to PORTB;
PORTC = shifter<<4; // Turn on appropriate 7seg. display;
//move shifter to next digit;
shifter <<= 1;
if(shifter > 8u)
shifter = 1;
//increment portb_index;
portb_index ++ ;
if (portb_index > 3u)
portb_index = 0; //turn on 1st, turn off 2nd 7 seg.;
if (my_timer > 0) my_timer--;
TMR2 = 0; //reset TIMER0 value;
TMR2IF = 0;
TMR2IE = 1; // Turn on TMR2 interrupt //clear T0IF, Bit T0IF=0, T0IE=1;
}
void InitTimer2(void)
{
T2CON = 0x3C;
/* Prescale 1:16
* Timer off for now
*/
PR2 = 249;
/* With prescale 1:16 and at 4MHz clock,
* this gives 2ms period.
*/
TMR2IF = 0;
TMR2IE = 1; // Turn on TMR2 interrupt
GIE = 1; // Turn on global interrupts
PEIE = 1; // Turn on peripheral interrupts
TMR2ON = 1; // Start TMR2
}
void InitIO(void)
{
digit = 0;
portb_index = 0;
shifter = 1;
PORTB = 0;
TRISB = 0;
PORTC = 0;
TRISC = 0;
ADCON1 =0x60;
ADCON0 = 0xC1;
PORTA = 0;
TRISA = 0x00; // Inputs for ADC
}
void UpdateDisplay(int display)
{
number=display;
digit = number % 10u; //extract ones digit;
portb_array[0] = mask(digit); //and store it to PORTB array;
digit = (number / 10u) % 10u; //extract tens digit;
portb_array[1] = mask(digit); //and store it to PORTB array;
digit = (number / 100u) % 10u; //extract hundreds digit;
portb_array[2] = mask(digit); //and store it to PORTB array;
digit = number / 1000u; //extract thousands digit;
portb_array[3] = mask(digit); //and store it to PORTB array;
}
void DisplayINPUT()
{
portb_array[0] = mask('I'); //and store it to PORTB array;
//portb_array[1] = mask(I); //and store it to PORTB array;
// portb_array[2] = mask(I); //and store it to PORTB array;
// portb_array[3] = mask('I'); //and store it to PORTB array;
}
float filter( float aData )
{
static float memory;
memory = memory*(1-K) + aData*K;
return memory;
}
unsigned int ADC_Read(unsigned char channel)
{
if(channel > 5) //If Invalid channel selected
return 0; //Return 0
ADCON0 &= 0xC5; //Clearing the Channel Selection Bits
ADCON0 |= channel<<3; //Setting the required Bits
__delay_ms(2); //Acquisition time to charge hold capacitor
GO_nDONE = 1; //Initializes A/D Conversion
while(GO_nDONE); //Wait for A/D Conversion to complete
return (ADRES); //Returns Result
}
void input()
{
adc=0;
for(i=0;i<20;i++) //adc=adc+result*1.96078;// 1s delay;
{
adc=adc+ ADC_Read(0);
}
result=19.6078431372549*(adc/20);//Endless loop;
UpdateDisplay( result);
__delay_ms(1000);
}
void output()
{
adc1=0;
for(i=0;i<20;i++) //adc=adc+result*1.96078;// 1s delay;
{
adc1=adc1+ ADC_Read(1);
}
result1=19.6078431372549*(adc1/20);//Endless loop;
UpdateDisplay( result1);
__delay_ms(1000);
}
main(void)
{
InitIO();
InitTimer2();
int i=1;
while(1)
{
switch(i)
{
case 1:
input();
break;
case 2:
DisplayINPUT();
case 3:
output();
case 4:
input();
break;
}
if (0==my_timer)
{
i++;
if (i>3) {
i = 0;
}
my_timer = 30 * 50; // MUX_FREQUENCY must be < 1092 Hz
}
}
}