C code of simple Receiver - c

I have this code of a transmitter and I have a problem where when I get input from my transmitter I save it in a char array "rec" and after I have finished receiving I compare it with my other arrays and base on that comparison I light up a bulb in portb. But the code gives me no errors and no bulb lights (the connection works I've tried it) it has something to do with the comparing arrays part. Am I doing this right? Thank you
char rec[3];
char cmp1[]= "1000";
char cmp2[]= "1010";
char cmp3[]="1111";
char cmp4[]="1001";
int i=0;
int beginrecord;
void main(void)
{
TRISB=0;
TRISD=0;
TRISC=255;
PORTC=0;
PORTB=0;
while(1)
{
if(PORTC==1)
{
rec[i]=1;
i++;
beginrecord = 1;
delay_ms(1);
}
if (PORTC==0 && beginrecord==1)
{
rec[i]=0;
i++;
delay_ms(1);
}
if(i==4) {
beginrecord = 0;
i = 0;
if(rec == cmp1){
portb = 0x01;}
else if(rec == cmp2){
portb = 0x02;}
else if(rec == cmp3){
portb = 0x04;}
else if(rec == cmp4){
portb = 0x08;}
}
}
}

You must use strcmp(rec, cmp1) == 0 instead of rec == cmp1.
The way you do it now, you compare only the pointers, which are never the same.
And are you sure that your port returns a character 1or 0 and not the values 0/1?
In that case your comparison also wouldn't work.
Some other issues with your code:
I assume that the char form the port is a binary value and not an ASCII character.
If it is indeed an ASCII character and you use strcmp() then you must add a zero byte at the end of the buffer.
If you receive multiple bytes and not bits, you must use memcmp() instead of strcmp(). In that case your intialisation of cmpX would be also wrong. If it are bits you receive, you would want to combine them into bytes before comparing (assuming a serial port).
PORTC is always 0, so it can not become 1 in your code.
You seem to have mixed case PORTB vs. portb.
your variables have no type.

Here you are declare three cmp[] of char type which is a string so it is like "1000". it is 4 byte data. and your rec[3] is only 3 byte. i think you try to compare a int type. like
1001 == 1001 in binary
Declare them as unsigned int then try to compare.
And make all your values full 8 bit like 00001000b not 1000.
or may be i get it wrong.

There are a number of issues that confuse your goal - see above comment, but I've tried to address them below.
Rather than accumulate the rec as an array, the bits are accumulated in an int. This simplifies the compares to your original style.
beginrecord needed initialization. Variables were brought into a local scope.
Cleaned up the indentation.
const int cmp1 = 8; // "1000";
const int cmp2 = 10; // "1010";
const int cmp3 = 15; // "1111";
const int cmp4 = 9; // "1001";
void main(void) {
TRISB = 0;
TRISD = 0;
TRISC = 255;
PORTC = 0;
PORTB = 0;
int i = 0;
int beginrecord = 0;
int rec = 0;
while (1) {
if (PORTC == 1) {
rec <<= 1; /* rec = rec * 2 */
rec |= 1;
beginrecord = 1;
delay_ms(1);
i++;
}
else if (PORTC == 0 && beginrecord == 1) {
rec <<= 1;
delay_ms(1);
i++;
}
if (i == 4) {
if (rec == cmp1) {
PORTB = 0x01;
} else if (rec == cmp2) {
PORTB = 0x02;
} else if (rec == cmp3) {
PORTB = 0x04;
} else if (rec == cmp4) {
PORTB = 0x08;
}
beginrecord = 0;
i = 0;
rec = 0;
}
}
}

Related

Programming of built in timer of arduino

I am new to programming in IDE. I am using built-in timer of arduino.I am using TDR method to capture reflection from fault location in cable and for that I am using timer.Timer should start as soon as output is send and stop at reflection. I have a reference code for it but I dont able to understand it, so if anyone know about it , it would be great.
void setup()
{
pinMode(stepPin, OUTPUT);
pinMode(refPin, OUTPUT);
pinMode(shutdownPin, OUTPUT);
TCCR1A = 0;
TCCR1B = (1 << ICNC1); // input capture noise canceller enabled, capture on falling edge
TIMSK1 = 0; // timer 1 interrupts disabled
ACSR = 0; // input capture from ICP1 pin
TCCR2B = (1 << CS20); // change timer 2 PWM frequency to 31.25kHz because we're using pin 11 as a DAC
Serial.begin(19200);
}
struct Step
{
unsigned int time;
unsigned int amplitude;
};
// Take a single measurement, using either a positive or negative edge from the comparator.
// The comparator reference voltage must have been set up and allowed to stablise before calling this.
unsigned int takeMeasurement(bool posEdge)
{
byte reg1b = (posEdge) ? 0 : (1 << ICES1); // input capture noise canceller csdisabled, set up input capture polarity, stop timer
reg1b |= (1 << CS10);
TCCR1B = reg1b;
TCNT1H = 0;
TCNT1L = 0; // clear timer
unsigned int capture = 0;
unsigned long start = micros(); // get the time
cli();
TCNT1H = 0;
TCNT1L = 0; // clear timer
TIFR1 = (1 << ICF1); // clear timer 1 input capture bit
PORTD |= (1 << 4); // set output high
sei();
do
{
if ((TIFR1 & (1 << ICF1)) && capture == 0)
{
byte temp = ICR1L;
capture = (ICR1H << 8) | temp;
}
} while (micros() - start < 100);
PORTD &= ~(1 << 4); // set output low
return capture;
}
size_t findSteps(bool positive, struct Step *results, size_t maxResults)
{
byte amplitude = (positive) ? 5 : 250;
analogWrite(refPin, amplitude);
delay(100); // wait 100ms for the output to stabilise
unsigned int lastReading = 0;
size_t numResults = 0;
unsigned int stepSize = 0; // 0 means not in a step
#ifdef DEBUG
Serial.print((positive) ? "pos " : "neg ");
#endif
for (int i = 0; i < 50; ++i)
{
analogWrite(refPin, amplitude);
delay(10);
unsigned int currentReading = takeMeasurement(positive);
unsigned int currentDiff = currentReading - lastReading; // diff since start of possible step
if (stepSize == 0)
{
// Not currently in a step
if (i != 0 && currentReading != 0 && currentDiff == 0)
{
// Found the start of a possible step
++stepSize;
}
lastReading = currentReading;
}
else
{
if (currentDiff > 2 || i + 1 == 50)
{
// Step has endeed, so record it if it is big enough
if (stepSize >= 2)
{
results->time = lastReading;
results->amplitude = amplitude - 5;
++results;
++numResults;
if (numResults == maxResults) break;
}
stepSize = 0;
lastReading = currentReading;
}
else if (currentDiff == 0)
{
++stepSize;
}
}
#ifdef DEBUG
if (i != 0) Serial.write(',');
Serial.print(currentReading);
#endif
if (positive)
{
amplitude += 5;
}
else
{
amplitude -= 5;
}
}
#ifdef DEBUG
Serial.println();
#endif
return numResults;
}

MPLAB XC8 Compiler PIC18F452 Multiplexed Seven Segment Display Code working properly

I am currently working on a code involving the MPLAB XC8 Compiler, PIC18F452 with a Multiplexed Seven Segment Display. I want to use two pushbuttons connected to pins RB2 and RB3 of PORTB of the PIC18F452 to increment and decrement a variable "count" and display this number from 0 to 99 on this display. Schematic and code is show below.
This code relatively functions as it is, and I do not believe the schematic is to blame for the issues I am seeing, nor is the byte array not correct as I am able to see each number when using the array with a 1 segment display.
The issue arrises when trying to use this multiplexing scheme shown in the below figure. I can successfully display two numbers on the seven segment displays, but there are strange anomalies present when executing this code. For one, I seem to not be able to display the number 1, 4 and occasionally 7 on either display, but when this digit does not show the display is blank, and when the button is again pushed the next number is shown as expected.
for example:
The display shows the numbers as follows for number sequences:
9... 10... 11... 12 13... 14... ect...
or
34.... 35... 36... 37....
Not sure where the issues lies, and debugging is not going well... any help would be appreciated.
Schematic for Multiplexed 7 Segment Display
#define _XTAL_FREQ 10000000
#include <xc.h>
#include <stdlib.h>
#define Digit1 PORTBbits.RB1 //variable to sink current to PNP base
#define Digit2 PORTBbits.RB2 //variable to sink current to PNP base
#define Switch1 PORTBbits.RB4 //switch decrement variable
#define Switch2 PORTBbits.RB3 //switch increment variable
#define Pressed1 1 //pressed is high
#define Pressed2 1 //pressed is high
void initialize();
void segment1 (void);
void segment2 (void);
void buttonPress(void);
void delay_ms(unsigned int);
void sendPattern1(unsigned char pattern);
void sendPattern2(unsigned char pattern3);
unsigned char rotateLeft(unsigned char pattern, int no);
unsigned char MSD, LSD, count=0;
Main code
void main(void){
initialize();
Digit1 = 0;
Digit2 = 0;
while(1){
buttonPress();
MSD = count/10 ;
segment1();
Digit2 = 1;
delay_ms(10); // Delay for 10 ms
Digit2 = 0;
LSD = count%10;
segment2();
Digit1 = 1;
delay_ms(10); // Delay for 10 ms
Digit1 = 0;
}
return;
}
Functions to index Most Significant Digit and Least Significant Digit from array to be sent to the ports to sink current low for common annode display.
void segment1(void){
unsigned char segArrayC[]={0b11000000,0b11111001,0b00100100,
0b00110000,0b00011001,0b00010010,
0b00000010,0b11111000,0b00000000,0b00011000};
unsigned char pattern;
pattern = segArrayC[MSD];
sendPattern1(pattern);
return;
}
void segment2(void){
unsigned char segArrayD[]= {0b11000000,0b11111001,0b00100100,
0b00110000,0b00011001,0b00010010,0b00000010,
0b11111000,0b00000000,0b00011000};
unsigned char pattern3;
pattern3 = segArrayD[LSD];
sendPattern2(pattern3);
return;
}
Button Press Code
void buttonPress(void){
if (Switch1 == Pressed1) {
++count;
delay_ms(100);
}
if (Switch2 == Pressed2) {
--count;
delay_ms(100);
}
if(count>=99||count<0)
{
count=0;
delay_ms(100);
}
return;
}
Function to rotate bytes in array two places to left to be displayed on PORTs
/** Rotate pattern to the left 'no' number of bits
*/
unsigned char rotateLeft(unsigned char pattern, int no) {
return (((pattern << no) & 0xFF) | (pattern >> (8-no)));
}
Functions to output indexed array char to PORTC and PORTB pins
void sendPattern1(unsigned char pattern) {
// Send pattern to appropriate port pins
unsigned char pattern2;
PORTC = pattern;
pattern2=rotateLeft(pattern, 2);
PORTB = pattern2;
return;
}
void sendPattern2(unsigned char pattern3) {
unsigned char pattern4;
PORTC = pattern3;
pattern4=rotateLeft(pattern3, 2);
PORTB = pattern4;
return;
}
Delay Function
void delay_ms(unsigned int n){
while (--n) _delay(2500);
}
Initialize pins to be used (0 output, 1 input)
void initialize() {
TRISC = 0;
TRISBbits.TRISB0 = 0;
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB4 = 1;
TRISBbits.TRISB3 = 1;
PORTC = 0x00;
PORTB = 0x00;
}
In sendPattern() you write a rotated bit pattern to PORTB.
This interferes with the setting the common anode control. So you see both digits only, if both right hand segments are turned on. According your schematic you should write a 0 to turn on the common anode. Try this:
void main()
{
static const unsigned char segArray[]=
{ 0b11000000, 0b11111001, 0b00100100, 0b00110000, 0b00011001,
0b00010010, 0b00000010, 0b11111000, 0b00000000, 0b00011000
};
TRISC = 0; //PortC all OUTPUT
PORTC = 0xFF; //PortC all HIGH = IDLE = LED_OFF
TRISBbits.TRISB0 = 0; //Output unused
TRISBbits.TRISB1 = 0; //Output Digit1
TRISBbits.TRISB2 = 0; //Output Digit2
TRISBbits.TRISB4 = 1; //Input: Switch PLUS
TRISBbits.TRISB3 = 1; //Input: Switch MINUS
PORTB = 0x00;
unsigned char count=0;
for(;;)
{
//Handle buttons
if (Switch1 && count<99)
{
++count;
delay_ms(100);
}
if (Switch2 && count > 0)
{
--count;
delay_ms(100);
}
//Write high digit
PORTC = segArray[count/10];
Digit2 = 0;
delay_ms(10); // Delay for 10 ms
Digit2 = 1;
//Write low digit
PORTC = segArray[count%10];
Digit1 = 0;
delay_ms(10); // Delay for 10 ms
Digit1 = 1;
}
}

Convert array of uint8_t into array of uint16_t (ASCII to Unicode)

I have a uint8_t array of size n>1 and would like to cast it into a uint16_t array of the same size n>1. I actually use the uint8_t array for ASCII characters and would like to use now UNICODE.
Any ideas how this conversion can be done?
Edit:
I would like to use this function here, which works fine for const char *string as argument and not for const uint16_t *string. So somehow I need to cast it.
srv_err_t gui_write_text_16bit(const uint16_t *string, Layout_type_t layout,
Layout_field_t field, Text_inverted_t inv) {
srv_err_t err;
uint8_t charCount;
uint8_t byteCount;
uint16_t bitmapCol = 0;
uint16_t bitmapRow = 0;
uint8_t textLength = 0;
uint8_t textHeight = GUI_FONT_NAME.FontHeight;
uint16_t offset;
uint8_t mask;
lcd_rectangle_t position;
if (LAYOUT_A == layout) {
if (LAYOUT_FIELD6 == field) {
// Position 6 is not available in Layout A
err.bits.input_parameter = true;
return err;
}
}
GUI_CONST_STORAGE GUI_CHARINFO
*pcharInfo;
GUI_CONST_STORAGE
unsigned char* pchar;
GUI_CONST_STORAGE GUI_FONT_PROP
*pfontProp;
//uint8_t textBitmap [bitmapLength * textHeight];
uint8_t textBitmap[(LCD_COLUMN_NUMBER_DISPLAY / 8) * GUI_FONT_HEIGHT] = { 0 };
/* Calculate needed space in the array */
// for (charCount = 0; charCount < stringLength; charCount++)
for (charCount = 0; string[charCount] != '\0'; charCount++) {
pfontProp = GUI_FONT_NAME.FontProp;
while (0 != pfontProp) {
if (pfontProp->First <= string[charCount]
&& pfontProp->Last >= string[charCount]) {
offset = string[charCount] - pfontProp->First;
pcharInfo = (pfontProp->pCharInfoFirstChar) + offset; // Pointer to the right character
textLength += pcharInfo->XSize; // Text length in Pixels
break; // exit while loop and beginn with next character
}
pfontProp = pfontProp->pNext;
}
}
textLength = (textLength / 8) + 1; // Text length in Bytes
// for(charCount = 0; charCount < stringLength; charCount++)
for (charCount = 0; string[charCount] != '\0'; charCount++) {
pfontProp = GUI_FONT_NAME.FontProp;
while (0 != pfontProp) {
if (pfontProp->First <= string[charCount]
&& pfontProp->Last >= string[charCount]) {
// Character in Range found
offset = string[charCount] - pfontProp->First;
pcharInfo = (pfontProp->pCharInfoFirstChar) + offset; // Pointer to the right character
pchar = pcharInfo->pData;
for (bitmapRow = 0; bitmapRow < textHeight; bitmapRow++) {
uint16_t bitmapByte = 0;
uint16_t charByte = 0;
uint8_t pixelShift;
for (byteCount = 0; byteCount < pcharInfo->BytesPerLine;
byteCount++) {
//bitmapByte = bitmapRow * bitmapLength + (bitmapCol / 8) + byteCount;
bitmapByte = bitmapRow * textLength + (bitmapCol / 8) + byteCount;
charByte = pcharInfo->BytesPerLine * bitmapRow + byteCount;
pixelShift = bitmapCol % 8;
if (byteCount == (pcharInfo->BytesPerLine - 1)) {
// Last Byte in row
switch (pcharInfo->XSize % 8) {
case 1:
mask = 0x80;
break;
case 2:
mask = 0xC0;
break;
case 3:
mask = 0xE0;
break;
case 4:
mask = 0xF0;
break;
case 5:
mask = 0xF8;
break;
case 6:
mask = 0xFC;
break;
case 7:
mask = 0xFE;
break;
case 0:
mask = 0xFF;
break;
default:
break;
}
textBitmap[bitmapByte] |= (pchar[charByte] & mask) >> pixelShift;
textBitmap[bitmapByte + 1] |= (pchar[charByte] & mask)
<< (8 - pixelShift);
//bitmapCol += pcharInfo->XSize % 8;
} else {
/* charByte is not aligned with the bitmapByte. A direct copy is not possible */
textBitmap[bitmapByte] |= pchar[charByte] >> pixelShift;
textBitmap[bitmapByte + 1] |= pchar[charByte]
<< (8 - pixelShift);
}
}
}
bitmapCol += pcharInfo->XSize;
break; // exit while loop and beginn with next character
}
pfontProp = pfontProp->pNext;
}
}
if (layout == LAYOUT_A) {
position = Layout_A_Text_Field[field];
/* place Bitmap on the right display Position */
if (LAYOUT_TITLE == field) {
gui_place_text(&position, textLength, textHeight, ALIGN_CENTER);
} else {
gui_place_text(&position, textLength, textHeight, ALIGN_LEFT);
}
if (LAYOUT_FIELD2 == field) {
lcd_draw_text(position, textBitmap, sizeof(textBitmap),
Layout_A_Field[field], DRAW_INVERSE);
} else {
lcd_draw_text(position, textBitmap, sizeof(textBitmap),
Layout_A_Field[field], DRAW_NORMAL);
}
}
return err;
}
Casting would be non sense because each element in a char array uses 1 single byte while 2 are needed for a UNICODE uint16_t array. But provided your initial string is composed of only ASCII or Latin1 characters, you can use the fact the the unicode code point of an ASCII or Latin1 character is just the character value.
So if the resulting array is allocated in caller, you could use code as simple as:
// Convert an ASCII or Latin1 string to a 16bits unicode string:
void tounicode(uint16_t *dest, const char *src, size_t nchars) {
while(nchars-- > 0) { // process nchars characters
*dest++ = (unsigned char) *src++; // one at a time
}
}
If you are using x86 hardware check out the SSE2 routines that convert a 16-byte vector into two 8-element uint16 vectors, in 2 machine ops. _mm_unpacklo_epi8() and ...unpackhi...

If vs Case. If incrementing strangely

I'm learning embedded programming, and have a LED board and button board hooked up to the same PORT.
The commented out if statement under the chk_button function, certain buttons when activated will add to the num variable its intended increment + 1. IE: pushing button 1 will increment num by three. The current case statement works to spec.
I understand case statements when compiled effectively become look up tables, and thus have smaller footprint and run faster,but I am uncertain as to WHY im getting the bug that I am.
I'm also open to any other criticisms in my code.
// HARDWARE SETUP:
// PORTA is connected to the segments of the LED display. and to the pushbuttons.
// PORTA.0 corresponds to segment a, PORTA.1 corresponds to segement b, etc.
// PORTB bits 4-6 go to a,b,c inputs of the 74HC138.
// PORTB bit 7 goes to the PWM transistor base.
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 16000000
#define DIGIT_ONE 0x00
#define DIGIT_TWO 0x10
#define DIGIT_COLON 0x20
#define DIGIT_THREE 0x30
#define DIGIT_FOUR 0x40
//******************************************************************************
// debounce_switches
//Checks the state of the button number passed to it. It shifts in ones till
//the button is pushed. Function returns a 1 only once per debounced button
//push so a debounce and toggle function can be implemented at the same time.
//Adapted to check all buttons from Ganssel's "Guide to Debouncing"
//Expects active low pushbuttons on PINA port. Debounce time is determined by
//external loop delay times 12.
//Saves status of button (i) into a state array.
//******************************************************************************
int8_t debounce_switches(uint16_t *state, uint8_t i) {
state[i] = (state[i] << 1) | (! bit_is_clear(PINA, i)) | 0xE000;
if (state[i] == 0xF000) return 1;
return 0;
}
//******************************************************************************
// chk_buttons
//Checks the buttons. Calls debounce_switches in a loop passing both the state array
//and the current switch being checked.
//If debounce_switches returns a 1 for a switch, case statements determine which switch
//was activated, and increments count by the appropriate value.
//******************************************************************************
void chk_buttons(uint16_t *state, uint16_t *num)
{
uint8_t itr;
for( itr=0; itr<8; itr++)
{
if( debounce_switches(state, itr))
{
switch(itr)
{
case 0:
*num += 1;
break;
case 1:
*num += 2;
break;
case 2:
*num += 4;
break;
case 3:
*num += 8;
break;
case 4:
*num += 16;
break;
case 5:
*num += 32;
break;
case 6:
*num += 64;
break;
case 7:
*num +=128;
break;
}
/*
if (itr == 0) *num += 1;
else if(itr == 1) *num += 2;
else if(itr == 2) *num += 4;
else if(itr == 3) *num += 8;
else if(itr == 4) *num += 16;
else if(itr == 5) *num += 32;
else if(itr == 6) *num += 64;
else if(itr == 7) *num += 128;
*/
}
}
}
//******************************************************************************
// itoseven
// Accepts a number from 0 -9 Returns a hex value to display on the seven segment..
//******************************************************************************
uint8_t itoseven(uint8_t num)
{
uint8_t segnum;
switch(num)
{
case 0:
segnum = ~0x3F;
return segnum;
case 1:
segnum = ~0x06;
return segnum;
case 2:
segnum = ~0x5B;
return segnum;
case 3:
segnum = ~0x4F;
return segnum;
case 4:
segnum = ~0x66;
return segnum;
case 5:
segnum = ~0x6D;
return segnum;
case 6:
segnum = ~0x7D;
return segnum;
case 7:
segnum = ~0x07;
return segnum;
case 8:
segnum = ~0x7F;
return segnum;
case 9:
segnum = ~0x6F;
return segnum;
}
}
//***********************************************************************************
// segment_sum
//takes a 16-bit binary input value and displays the result to the LED board.
//***********************************************************************************
void segsum(uint16_t num) {
uint8_t ones;
uint8_t tens;
uint8_t hundreds;
uint8_t thousands;
//break up decimal sum into 4 digit-segments
ones = num % 10;
tens = (num/10)%10;
hundreds = (num/100)%10;
thousands = (num/1000)%10;
//determine how many digits there are
if(num < 10)
{
PORTB = DIGIT_ONE;
PORTA = itoseven(num);
}
else if(num > 9 && num <100)
{
PORTB = DIGIT_ONE;
PORTA = itoseven(ones);
_delay_ms(2);
PORTB = DIGIT_TWO;
PORTA = itoseven(tens);
}
else if(num > 99 && num < 1000)
{
PORTB = DIGIT_ONE;
PORTA = itoseven(ones);
_delay_ms(2);
PORTB = DIGIT_TWO;
PORTA = itoseven(tens);
_delay_ms(2);
PORTB = DIGIT_THREE;
PORTA = itoseven(hundreds);
}
else if (num >999)
{
PORTB = DIGIT_ONE;
PORTA = itoseven(ones);
_delay_ms(2);
PORTB = DIGIT_TWO;
PORTA = itoseven(tens);
_delay_ms(2);
PORTB = DIGIT_THREE;
PORTA = itoseven(hundreds);
_delay_ms(2);
PORTB = DIGIT_FOUR;
PORTA = itoseven(thousands);
}
}
uint8_t main()
{
DDRB = 0xF0; //set port bits 4-7 B as outputs
uint16_t num = 0;
uint16_t state[8];
//initialize array values for debouncing
for(int i=0; i<8; i++)
{
state[i]= 0;
}
while(1)
{
//make PORTA an input port with pullups
DDRA = 0x00;
PORTA = 0xFF;
//enable tristate buffer for pushbutton switches
PORTB = 0x70;
//check the buttons. Increment by appropriate value if switch is active.
chk_buttons(&state, &num);
//rollover at 1023
if(num > 1023)
num = 1;
//switch PORTA to output
DDRA = 0xFF;
//Send num to display
segsum(num);
_delay_ms(2);
}
return 0;
}
I've update these functions to remove the switches as they are not needed at all. Note how the first uses an algorithm in place of a switch and the second uses a table that is easy to adjust in the future. Also, assert/error check your inputs and you'll be a happier camper when things go sideways.
void chk_buttons(uint16_t *state, uint16_t *num)
{
// Assert bad state and num here.
uint8_t itr;
for( itr=0; itr<8; itr++)
{
if( debounce_switches(state, itr))
{
*num += 1 << itr; // Could also use |= as that is what we are doing.
}
}
}
uint8_t itoseven(uint8_t num)
{
// assert num <= 9
const uint8_t kLookup[10] =
{~0x3f, ~0x06, ~0x5B, ~0x4F, ~0x66,
~0x6D, ~0x7D, ~0x07, ~0x7F, ~0x6F};
return kLookup[num];
}
For the last function, if you start at the 4-digit number check and work backwards to the single digit, it becomes a very simple loop to do your complex digit calculation. Just divide your mask from 10,000 to 10. I'll post that code a bit later as I have time.
Update
Here is something I coded up that demonstrates the idea. You will need to modify it for your own functions as right now it just outputs ASCII. It fails for negative numbers (as does your code), but that is very easy to add with a simple if check / ABS / prepend a minus character.
int main(void)
{
int num = 9876;
// Starting at the 5th digit. You could use 1000 instead if
// there were only 4 digits or less ever.
int digitMask = 10000;
char output[16];
int index = 0;
// Handle 0 case.
if(num== 0)
{
output[index++] = '0';
}
else
{
// Skip leading 0's
while((num / digitMask) == 0)
{
digitMask /= 10;
}
// While there may be digits to process...
while(digitMask)
{
// Grab MS Digit and mod it away from total.
int digit = num / digitMask;
num %= digitMask;
// Convert to ASCII (you would use your own function)
output[index++] = digit + '0';
// Update mask - note we could use a for/loop here just as easily.
digitMask /= 10;
}
}
// Terminate string and print it.
output[index] = 0;
printf("%s", output);
return 0;
}

8051 c interrupts

I'm using C to write a program on an 8051 microcontroller. The compiler I'm using is Keil Microvision. I'm stuck and having trouble figuring out what is missing from my code. I know it's very basic code I just can't figure out what I'm supposed to do.
So pretty much what I am doing is taking sending a sentence out to the user and having them answer yes or no through the serial port and I used a serial interrupt. That part works fine. If I get a no from the person I want to generate a square wave 5kHz by a timer interrupt. I want this square wave to be controlled by an external interrupt turning it on and off when the external interrupt on pin P3.2 is either on or off.
Here is all my code
#include <REG52.H>
#include <stdio.h>
sbit WAVE = P1 ^ 7;
#define BIT(x) (1 << (x))
void timer0() interrupt 1 // timer is controlling square wave timer 0
{
WAVE = ~WAVE;
}
void interrupt0() interrupt 0
{
IE ^= BIT(1);
}
void serial0() interrupt 4
{
unsigned char x;
unsigned int i, z;
unsigned char yes[] = " YES ";
unsigned char no[] = " NO ";
unsigned char nvalid[] = " NOT VALID TRY AGAIN ";
while (RI == 1) {
x = SBUF;
RI = 0;
if (z < 1) {
if (x == 'n') {
for (i = 0; i < 4; i++) {
SBUF = no[i];
while (TI == 0) ; //wait for transmit
TI = 0;
z++;
}
}
} else {
return;
}
if (x == 'y') {
for (i = 0; i < 5; i++) {
SBUF = yes[i];
while (TI == 0) ;
TI = 0;
}
} else if (x != 'n') {
for (i = 0; i < 21; i++) {
SBUF = nvalid[i];
while (TI == 0) ;
TI = 0;
}
}
TI = 0;
return;
}
}
void main()
{
TMOD = 0x20;
TH1 = 0xF6; //baud rate
SCON = 0x50;
TH0 = 0xA4;
IE = 0x93; //enable interrupts
IP = 0x10; // propriety to serial interrupt
TR1 = 1; //start timer 1
TR0 = 1; //clear timer 0
TI = 1;
printf("Hello, Are you okay? Press y for yes and n for no ");
while (1) ;
}
The part I'm having trouble with is these two interrupt from the previous code
void timer0() interrupt 1 // timer is controlling square wave timer 0
{
WAVE=~WAVE;
}
void interrupt0() interrupt 0
{
IE ^= BIT(1);
}
Any hints in the right direction would be greatly appreciated! Thanks. Sorry about formatting
Variables that are modified by interrupts should be defined as volatile:
volatile sbit WAVE = P1 ^ 7;

Resources