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.
PORTC = (PORTC & 0x04) | digits[0];
output_c ( PORTC ) ;
#byte PORTC = 0xF82
#byte PORTC = 0x08
PORTB.6 = (digits[0] & 0x04) >> 2;
#define _XTAL_FREQ 4000000UL
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#pragma config FOSC = INTRC_IO // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config MCLRE = OFF // Master Clear Enable bit (MCLR disabled)
#pragma config IOSCFS = 4MHz // Internal Oscillator 4 MHz
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR disabled)
#pragma config CPSW = OFF // Code Protection Self Write disabled
#pragma config DRTEN = OFF // Device Reset Timer Enable (disabled)
#pragma config CP = ON // Flash Program Memory Code Protection bit (Code protection off)
#define SSD_DATA_PORT PORTC
#define OPAMP_OUTPUT 0
#define DECIMAL_POINT_MASK 0x80
#define ON 1
#define OFF 0
#define SET 1
#define CLEAR 0
#define TRUE 1
#define FALSE 0
bit run_once_flag = 0;
unsigned char digit = 0;
unsigned char digits[3] = {0, 0, 0};
double raw_adc_value = 0;
const unsigned char cc_mask[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
void InitTimer0(void);
void Get_Mask(double fpNum);
//Timer0
//Prescaler 1:8; TMR0 Preload = 6; Actual Interrupt Time : 2 ms
//Place/Copy this part in declaration section
void InitTimer0(void) {
OPTION = 0x82;
TMR0 = 6;
INTCON0 = 0x01;
}
void interrupt isr(void) {
if(INTCON0bits.T0IF) {
//Enter your code here
SSD_DATA_PORT = (SSD_DATA_PORT & 0x04) | 0xFF;
PORTBbits.RB6 = 1;
PORTAbits.RA1 = 1;
PORTAbits.RA2 = 1;
PORTBbits.RB7 = 1;
switch(digit) {
case 0:
SSD_DATA_PORT = (SSD_DATA_PORT & 0x04) | digits[0];
PORTBbits.RB6 = (digits[0] & 0x04) >> 2;
PORTAbits.RA1 = 0;
break;
case 1:
SSD_DATA_PORT = (SSD_DATA_PORT & 0x04) | digits[1];
PORTBbits.RB6 = (digits[1] & 0x04) >> 2;
PORTAbits.RA2 = 0;
break;
case 2:
SSD_DATA_PORT = (SSD_DATA_PORT & 0x04) | digits[2];
PORTBbits.RB6 = (digits[2] & 0x04) >> 2;
PORTBbits.RB7 = 0;
break;
};
if(++digit == 3) {
digit = 0;
}
INTCON0bits.T0IF = 0;
TMR0 = 6;
}
}
unsigned char ADC_Read(unsigned char channel) {
if(channel > 7) //Channel range is 0 ~ 7
return 0;
ADCON0 &= 0xC5; //Clearing channel selection bits
ADCON0 |= channel<<3; //Setting channel selection bits
__delay_ms(2); //Acquisition time to charge hold capacitor
GO_nDONE = 1; //Initializes A/D conversion
while(GO_nDONE); //Waiting for conversion to complete
return ADRES; //Return result
}
void Get_Mask(double fpNum) {
unsigned char whole_num = 0, fraction = 0;
double f_fraction = 0.0;
if((fpNum >= 0.0) && (fpNum < 1.0)) {
f_fraction = (unsigned char)fpNum * 100.0;
fraction = (unsigned char)f_fraction;
digits[0] = cc_mask[0] | DECIMAL_POINT_MASK;
digits[1] = cc_mask[fraction / 10];
digits[2] = cc_mask[fraction % 10];
}
else if((fpNum >= 1.0) && (fpNum < 10.0)) {
whole_num = (unsigned char)fpNum;
f_fraction = (fpNum * 100.0) - (whole_num * 100.0);
fraction = (unsigned char)f_fraction;
digits[0] = cc_mask[whole_num] | DECIMAL_POINT_MASK;
digits[1] = cc_mask[fraction / 10];
digits[2] = cc_mask[fraction % 10];
}
else if((fpNum >= 10.0) && (fpNum < 100.0)) {
whole_num = (unsigned char)fpNum;
f_fraction = (fpNum * 10.0) - (whole_num * 10.0);
fraction = (unsigned char)f_fraction;
digits[0] = cc_mask[whole_num / 10];
digits[1] = cc_mask[whole_num % 10] | DECIMAL_POINT_MASK;
digits[2] = cc_mask[fraction];
}
else if((fpNum >= 100.0) && (fpNum < 1000.0)) {
whole_num = (unsigned char)fpNum;
digits[0] = cc_mask[whole_num / 100];
digits[1] = cc_mask[(whole_num / 10) % 10];
digits[2] = cc_mask[whole_num % 10];
}
}
int main(void) {
CM1CON0 = 0x00;
CM2CON0 = 0x00;
ANSEL = 0x01;
ADCON0 = 0x00;
TRISA = 0x01;
TRISB = 0x30;
TRISC = 0x00;
PORTA = 0x06;
PORTB = 0x80;
PORTC = 0x00;
__delay_ms(200);
OPACON = 0x01;
InitTimer0();
while(1) {
raw_adc_value = (double)ADC_Read(OPAMP_OUTPUT) * 150.0 / 255.0;
__delay_ms(5);
Get_Mask(raw_adc_value);
if(run_once_flag == 0) {
INTCON1bits.T0IE = 1;
run_once_flag = 1;
}
}
return (0);
}
#include <main.h>
#byte PORTC = 0x08
#define OPAMP_OUTPUT 0
#define DECIMAL_POINT_MASK 0x80
unsigned char run_once_flag = 0;
unsigned char digit = 0;
unsigned char digits[3] = {0, 0, 0};
double raw_adc_value = 0;
const unsigned char cc_mask[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
void Get_Mask(double fpNum);
#int_timer0
void TIMER0_isr() {
//Enter your code here
output_c((PORTC & 0x04) | 0xFF);
output_high(PIN_B6);
output_high(PIN_A1);
output_high(PIN_A2);
output_high(PIN_B7);
switch(digit) {
case 0:
output_c((portc & 0x04) | digits[0]);
//PORTB.6 = (digits[0] & 0x04) >> 2;
output_low(PIN_A1);
break;
case 1:
output_c((portc & 0x04) | digits[1]);
//PORTB.6 = (digits[1] & 0x04) >> 2;
output_low(PIN_A2);
break;
case 2:
output_c((portc & 0x04) | digits[2]);
//PORTB.6 = (digits[2] & 0x04) >> 2;
output_low(PIN_B7);
break;
};
if(++digit == 3) {
digit = 0;
}
clear_interrupt(INT_TIMER0);
}
void Get_Mask(double fpNum) {
unsigned char whole_num = 0, fraction = 0;
double f_fraction = 0.0;
if((fpNum >= 0.0) && (fpNum < 1.0)) {
f_fraction = (unsigned char)fpNum * 100.0;
fraction = (unsigned char)f_fraction;
digits[0] = cc_mask[0] | DECIMAL_POINT_MASK;
digits[1] = cc_mask[fraction / 10];
digits[2] = cc_mask[fraction % 10];
}
else if((fpNum >= 1.0) && (fpNum < 10.0)) {
whole_num = (unsigned char)fpNum;
f_fraction = (fpNum * 100.0) - (whole_num * 100.0);
fraction = (unsigned char)f_fraction;
digits[0] = cc_mask[whole_num] | DECIMAL_POINT_MASK;
digits[1] = cc_mask[fraction / 10];
digits[2] = cc_mask[fraction % 10];
}
else if((fpNum >= 10.0) && (fpNum < 100.0)) {
whole_num = (unsigned char)fpNum;
f_fraction = (fpNum * 10.0) - (whole_num * 10.0);
fraction = (unsigned char)f_fraction;
digits[0] = cc_mask[whole_num / 10];
digits[1] = cc_mask[whole_num % 10] | DECIMAL_POINT_MASK;
digits[2] = cc_mask[fraction];
}
else if((fpNum >= 100.0) && (fpNum < 1000.0)) {
whole_num = (unsigned char)fpNum;
digits[0] = cc_mask[whole_num / 100];
digits[1] = cc_mask[(whole_num / 10) % 10];
digits[2] = cc_mask[whole_num % 10];
}
}
void main() {
setup_comparator(NC_NC_NC_NC);
setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_tris_a(0x01);
set_tris_b(0x30);
set_tris_c(0x00);
output_a(0x06);
output_b(0x80);
output_c(0x00);
delay_ms(200);
setup_opamp1(OPAMP_ENABLED);
setup_timer_0(T0_INTERNAL | T0_DIV_8 | T0_8_BIT);
while(TRUE) {
//TODO: User Code
raw_adc_value = (double)read_adc(OPAMP_OUTPUT) * 150.0 / 255.0;
delay_ms(5);
Get_Mask(raw_adc_value);
if(run_once_flag == 0) {
enable_interrupts(INT_TIMER0);
run_once_flag = 1;
}
}
}
#byte PORTC = 0x08
#byte PORTC = getenv("sfr:PORTC ")
SSD_DATA_PORT = (SSD_DATA_PORT * 0x04) | 0xff;
SSD_DATA_PORT = (SSD_DATA_PORT & 0x04) | digits[0];
SSD_DATA_PORT = 0x04 | digits[0];
I couldn't see the pic16f527.h and pic16f527_registers.h in your codes.
#include <16F527.h>
#device ADC=8
#FUSES NOWDT //Watch Dog Timer
#use delay(crystal=4000000)
#use FIXED_IO( A_outputs=PIN_A2,PIN_A1 )
#use FIXED_IO( B_outputs=PIN_B7,PIN_B6 )
#use FIXED_IO( C_outputs=PIN_C7,PIN_C6,PIN_C5,PIN_C4,PIN_C3,PIN_C2,PIN_C1,PIN_C0 )
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 #define SSD_DATA_PORT LATC #define OPAMP_OUTPUT 0 #define DECIMAL_POINT_MASK 0x7F const unsigned char ca_mask[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90}; unsigned char myFlags = 0; unsigned char digit = 0; unsigned char digits[3] = {0, 0, 0}; unsigned int whole_num = 0, fraction = 0; double f_fraction = 0.0; double raw_adc_value = 0.0, previous_raw_adc_value = -1.0; sbit run_once_flag at myFlags.B0; //Timer1 //Prescaler 1:1; TMR1 Preload = 63536; Actual Interrupt Time : 2 ms //Place/Copy this part in declaration section void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = 0xF8; TMR1L = 0x30; INTCON = 0xC0; } void interrupt() { if(TMR1IF_bit) { //Enter your code here //SSD_DATA_PORT = (SSD_DATA_PORT | 0x04) & 0xFB; LATA.F1 = 1; LATA.F2 = 1; LATB.F7 = 1; switch(digit) { case 0: //SSD_DATA_PORT = (SSD_DATA_PORT & 0x04) | digits[0]; SSD_DATA_PORT = digits[0]; LATB.F6 = (digits[0] & 0x04) >> 2; LATA.F1 = 0; break; case 1: //SSD_DATA_PORT = (SSD_DATA_PORT & 0x04) | digits[1]; SSD_DATA_PORT = digits[1]; LATB.F6 = (digits[1] & 0x04) >> 2; LATA.F2 = 0; break; case 2: //SSD_DATA_PORT = (SSD_DATA_PORT & 0x04) | digits[2]; SSD_DATA_PORT = digits[2]; LATB.F6 = (digits[2] & 0x04) >> 2; LATB.F7 = 0; break; }; if(++digit >= 3) { digit = 0; } TMR1IF_bit = 0; TMR1H = 0xF8; TMR1L = 0x30; } } void main() { //OPTION_REG = 0x8F; asm clrwdt CM1CON0 = 0x00; CM2CON0 = 0x00; ANSELA = 0x01; ANSELB = 0x00; ANSELC = 0x00; ADCON1 = 0b11010000; ADCON2 = 0x00; TRISA = 0x01; TRISB = 0x30; TRISC = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; LATA = 0x06; LATB = 0x80; LATC = 0xFB; asm clrwdt Delay_ms(200); asm clrwdt OPA1CON = 0xC0; OPA2CON = 0x00; InitTimer1(); while(1) { asm clrwdt raw_adc_value = ADC_Read(OPAMP_OUTPUT) * 150.0 / 1023.0; Delay_ms(5); if(previous_raw_adc_value != raw_adc_value) { if((raw_adc_value >= 0.0) && (raw_adc_value < 1.0)) { f_fraction = (unsigned int)raw_adc_value * 100.0; fraction = (unsigned int)f_fraction; asm clrwdt digits[0] = ca_mask[0] & DECIMAL_POINT_MASK; digits[1] = ca_mask[fraction / 10]; digits[2] = ca_mask[fraction % 10]; } else if((raw_adc_value >= 1.0) && (raw_adc_value < 10.0)) { whole_num = (unsigned int)raw_adc_value; f_fraction = (raw_adc_value * 100.0) - (whole_num * 100.0); fraction = (unsigned int)f_fraction; asm clrwdt digits[0] = ca_mask[whole_num] & DECIMAL_POINT_MASK; digits[1] = ca_mask[fraction / 10]; digits[2] = ca_mask[fraction % 10]; } else if((raw_adc_value >= 10.0) && (raw_adc_value < 100.0)) { whole_num = (unsigned int)raw_adc_value; f_fraction = (raw_adc_value * 10.0) - (whole_num * 10.0); fraction = (unsigned int)f_fraction; asm clrwdt digits[0] = ca_mask[whole_num / 10]; digits[1] = ca_mask[whole_num % 10] & DECIMAL_POINT_MASK; digits[2] = ca_mask[fraction]; } else if((raw_adc_value >= 100.0) && (raw_adc_value < 1000.0)) { whole_num = (unsigned int)raw_adc_value; asm clrwdt digits[0] = ca_mask[whole_num / 100]; digits[1] = ca_mask[(whole_num / 10) % 10]; digits[2] = ca_mask[whole_num % 10]; } if(run_once_flag == 0) { TMR1IE_bit = 1; run_once_flag = 1; } asm clrwdt previous_raw_adc_value = raw_adc_value; } } }