# ra68gi

EstablishedMember

229

1. ## What Value Do I Load Onto Tmr0 ?

Trossin, I live in India which is seeing tremendous industrial & economic growth and with that comes a huge energy demand. Unable to meet with the peak demand many a times the frequency dips far below the normal( 50 Hz) in our place. Line frequency as clock is out of question. But your second option looks great. I should check out if such a clock is available here. Regards Raghunathan.
2. ## What Value Do I Load Onto Tmr0 ?

You still have a problem. You have it fixed in your head that the number TMR0 needs to count from is 250. This is incorrect. The correct number is 249 because this number will cause TMR0 to go through 250 count states and then set the TMR0IF flag. Using tmr0+=8; to set the period of TMR0 will cause it to use 251 count states. Think of the TMR0 as if it's an index of an array in C. If the array is 250 elements then the index of the first element is 0 and the last is 249. <{POST_SNAPBACK}> Hi ca001, i think, you have missed a few lines of my explaination. We all know that tmr0 will show values from 0-255 only. If you count from 0-255 you will get only 255 counts and not 256. Only when the counter counts back from 255-0, would we count 256. Check it out. A small Quick check. Let's have a counter upto 5. 0-1..........1 count 1-2...........2 2-3............3 3-4.............4 4-5...............5 now 5-0.................6 So if you count upto 249, you make 249 counts or ticks o.k. But you can not make tmr0 register to count upto 249 and then jump to 0. Because tmr0 will generate interrupt only when the counter changes from 255 to 0. So what we do, we make it to start counting from 6. AS you say 255-6=249. So you have your magical no.249. And now when the counter jumps to 0 from 255, eventually you have counted 250. Again we know that the counter sleeps for 2 ticks upon writing 6 to tmr0. but that does not mean that the real time isn't changing. This elapsed 2 clock cycles of real time is also added to 6 and so becomes 8. I hope this explaination satisfies you. Regards Raghunathan.

4. ## What Value Do I Load Onto Tmr0 ?

There is no compenastion for any code execution delays. You have todo that in your code (maybe you even need to use inline assembler to gurantee that the code will never change as could happen with some new optimisation). The simulation of TMR0 works like the hardware Regards Dave <{POST_SNAPBACK}> Dave, you say that the simulator works just like the hardware. Than if the hardware takes 2 clock cycles for a write operation onto tmr0, your simulator too must take 2 clock cycles. Am I right? Raghunathan.
5. ## What Value Do I Load Onto Tmr0 ?

I confess i am new to c pogramming, but found it very simple to implement it here. Why sould you reset the timer in the ISR. The ISR should just update the necessary flags. The most important of which is the timer over flow flag(intcon.2). <{POST_SNAPBACK}> Hmm this differs slightly from what the datasheet says and what you do... TMR0IF must be cleared before reenabling the Timer0 Interrupt, nothing about it automatically happening. Looking at your code i expect that the timer will run once and then stop. If the datasheet is right then as soon as you enter the TMR0 interrupt you should clear the flag and reenable it so that it runs again right away with the instruction losses for the ISR calculated in. Used to using Timer1 myself on chips that need a counter reload everytime so this is a normal process to me <{POST_SNAPBACK}> Hi emte, My code works beautifully on the simulator. TMR0IF is the the same as intcon.2, we also call it as timer0 over flow flag( also called timer0 interrupt flag). I have cleared this flag at the end of the code in the ISR(interrupt service routine) void interrupt(void). Hi cac001, If your talking of + or - 3 clock pulses, i think my code will do the job or may be better. If the source boost simulator gives an accurate value of the latency in tmr0 upon entering the ISR & if it also gives the right value upon execution of the c code tmr0 += x; Then my clock should be accurate. I think i have one more question for Dave. Dave, you told me that the compiler does not compensate for a write operation on the timer0 but does the simulator compensate for it? I think it compensates for the write operation & also the extra lines of asm code that it would generate for the c instruction tmr0+=x;. cac001, the incrementing of seconds, minutes, hours are generally done in the ISR itself. I am not unique. See Trossin's code. Regards Raghunathan.
6. ## What Value Do I Load Onto Tmr0 ?

Thanks Trossin & Dave. I wanted to do make a beginner level project & so started of with the relatively simpler tmr0 & without having to set the PIR & PIE. I see that i can't ask for more accuracy from it. But after doing so much I think i should go a little further and implement timer2 for more accuracy. Emte, Yes. The data sheet says if tmr0 is written(add to or subtracted from or just write a new value), the increment of the timer is inhibited for the following two clock cycles. ie. to say it takes 2micro seconds( in our case with 4mhz crystal) for the timer0 to get your value on its register. I confess i am new to c pogramming, but found it very simple to implement it here. Why sould you reset the timer in the ISR. The ISR should just update the necessary flags. The most important of which is the timer over flow flag(intcon.2). I don't stop the timer. It starts running as soon as you set the option register. And it sets the over flow flag when ever the the tmr0 rolls over from 255 to 0. There is no question of resetting your timer. With out any write operation the timer0 will clock 256 microsecons(4mhz crystal) on every interrupt(at every roll over). We just have to see that it gives us an interrut at every 250 microsecond for which we advance the tmr0 by 6micro(256-6) or add 6 to timer0(tmr0+=6;). I think that explains your doubt. On implimenting i faced with problem & thats explained in my first post. Regards, Raghunathan.

8. ## How To Setup A Software Interrupt?

Hi cac001, I prefer using the At89c2051 to do my timing requirements. I use the bascom basic compiler. Its got a cool feature called auto reload, in which timer0 register gets automatically reloaded with the required value. Say you want an interrupt every 250 micro seconds( if crystal freq,f=12mhz then clock freq=f/12=1mhz, for 2051),the auto reload feature pre loads the timer0 to 6. When the timer0 over flows & an interrupt occurs, the timer0 register is instantaneously loaded to 6 even before it saves all the registers & special function registers(sfr) and enters the interrupt service routine(isr), there by you dont miss any tick. This feature is not there with our Pics and we need to manually load the timer0 value. This being the case is it o.k to preload the timer0 to 6 and then on every interrupt we add 6 to the timer0 ( over flow that was caused due to the lattency in saving those files etc.) register in the interrupt service routine(isr)? Is this the way you have done in your program? Should it be 6 or 7, if it takes 1mico(one clock cycle) to add value to the timer0 register? Regards Raghunathan.

12. ## Maths In Boostc Giving Me A Headache.....

Hi guys, Will this work? Instead of using two 16 bit registers temp1 & temp2, why dont we load it directly? let's say the adc result is right justified. then result=adresl; result.8=adresh.0; result.9=adresh.1; Raghunathan.
13. ## Problem With Port A Of Pic16f876

Hi cac001, PIC16F876 has got both adc and comparator connected to porta. The cmcon register is by default sets to off or 7 on POR. So on power-on reset (POR) porta gets configured as analog input for the adc. In order to change it to digital you should config adcon1 to 7. Regards Raghunathan
14. ## Problem With Port A Of Pic16f876

Hi, As far as i know PIC16F876 is a 28 pin chip. To see your leds work on porta make adcon1=7; ie. set it to digital mode. I would suggest you to take a look at the thread "pic micro programming in BoostC for beginners". Regards Raghunathan.
15. ## "pic Micro Programming In Boostc For Beginners"

Your using one pin? Your code says your using 5 pins... But i am curious, how are you resolving to +/- 0.1 degree with that small a range ... <{POST_SNAPBACK}> Hi emte, rfhaley is using 5 sensors for the 5 channel adc and thats why its 5 pins. you can set just 1 pin to adc if you want. I don't know if its a 10bit adc or 12 bit adc. any way he is not using adresl. Very soon i will be posting a thermometer project using LM34 sensor on my thread "Pic Micro programming in Boostc for beginners". Just not finding enough time. regards, Raghunathan.
17. ## PIC Basic pulsout function

Fred, In fact my sample code as a macro compiles to just 30 words & each addition pulseout statement increases the code just by 5 words. Its just like any other library function used in other compilers like pic basic. Raghunathan.
18. ## PIC Basic pulsout function

Hi Fred, Your code compiles to 129 words. I checked it up on the simulator & found no change on the LED. ? . Regards Raghunathan.
19. ## PIC Basic pulsout function

That solves my problem. Using macros is a cool feature. But Dave, I did not see this style or syntax in BoostC user's manual. Instead I found template <class T> void foo(T *t) { ..... } Can you please elabrate as to how one could use this syntax in my example code. Thanks, Raghunathan.
20. ## PIC Basic pulsout function

Hi Guys, I wanted to create a library function similar to the pulsout command of pic basic compiler. In basic the command is like this pulsout pin, period The pin is any portb pin & period is duration for which it is high. example pulsout portb.0,100 I tried to create this in C using the references as function arguments. Let me quote from the Boost C user manual which says that when such argument changes in side the function the original variable used in the function call changes too. Below is my sample code for pulseout command. #include <system.h> #pragma CLOCK_FREQ 4000000 // config clock to 4mhz. // Set configuration fuse. #pragma DATA _CONFIG, _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF void pulseout(unsigned char &pin,unsigned char duration); void main() { trisb=0; portb=0; while( 1 ) { pulseout(portb.0,1); } } void pulseout(unsigned char &pin,unsigned char duration) { pin=1; delay_ms(duration); pin=0; } I tried this on the simulator but did not see portb.0 change to 1 when pin=1; gets executed in the pulseout function. But i found that portb.0 takes the value of the last statement executed, In this case its 0. If you make pin=1; as the last statement, port.0 is made 1. Is there any way we can make the original variable change in real time as and when the argument gets changed? There by i can make any port pin to out pulses. Very useful in LCD, SPI etc programs where this function gets called several times. Raghunathan.
21. ## "pic Micro Programming In Boostc For Beginners"

/* 3-digit-7-segment_display (Using PIC16F84) In this project we will connect three common cathode 7-segment modules in parallel & then connect them to the 7 pins of portb(RB1thro'RB7) via 470 ohms resistors. In all our future projects we will use RB1 TO RB7 pins for our 7 LEDs of the segment module. We will demultiplex the data to the 7-segment modules by using three transistors connected to the common cathode pin of each of the module. The base of the transistors are driven from porta pins. This project will form the base for our future projects like A/D converter or a digital thermometer, digital clock etc. The C program will display decimal 0 to 999 on the three segments. The program is in a loop and so will continue to display from 0 once again after it reaches the value off 999. We can't use the simulator for this project. So we have to hard wire to see our code work. I have checked it on my board. You can probably use a bread board to test it. Connections: PIC16F84 pin4- MCLR pin to +5v via 4.7K resistor. pin15-16 4mhz crystal(across pin 15 & 16). connect 22pf capacitors( between pin15 & gnd, pin16 & gnd). pin6-RB0-no connection. pin7-RB1-connect to segment "e" of the 7-segment display via 470 ohms. pin8-RB2-connect to segment "a" of the 7-segment display via 470 ohms. pin9-RB3-connect to segment "f" of the 7-segment display via 470 ohms. pin10-RB4-connect to segment "c" of the 7-segment display via 470 ohms. pin11-RB5-connect to segment "g" of the 7-segment display via 470 ohms. pin12-RB6-connect to segment "b" of the 7-segment display via 470 ohms. pin13-RB7-connect to segment "d" of the 7-segment display via 470 ohms. pin14-connect to +5v supply. pin5- connect to ground. Base of transistor(U) is connected to RA2(pin1 of micro) via 2.2k resistor. Base of transistor(T) is connected to RA1(pin18 of micro) via 2.2k resistor. Base of transistor(H) is connected to RA0(pin17 of micro) via 2.2k resistor. Seven segment modules. The segment pins of the three 7-segment module must be paralled,ie. all "a" segments are connected together & all "b" segments are connected & so on. Let's call them the units digit,tens digit & hundreds digit. Common cathode of units digit is connected to transistor(U)'s collector. Emitter of transistor(U) is connected to ground. Base of transistor(U) is connected to RA2(pin1 of micro) via 2.2k resistor. Common cathode of tens digit is connected to transistor(T)'s collector. Emitter of transistor(T) is connected to ground. Base of transistor(T) is connected to RA1(pin18 of micro) via 2.2k resistor. Common cathode of hundreds digit is connected to transistor(H)'s collector. Emitter of transistor(H) is connected to ground. Base of transistor(H) is connected to RA0(pin17 of micro) via 2.2k resistor. Transistor type-BC 547 NPN. */ #include <system.h> #pragma CLOCK_FREQ 4000000 // Set configuration word #pragma DATA _CONFIG, _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF //declare function get_value. unsigned char get_value(unsigned char y); unsigned char y; unsigned char units; //declare units as unsigned char variable. unsigned char tens; //declare tens as unsigned char variable. unsigned char hundreds; //hundreds as unsigned char variable. void main(void) { trisa=0; //make porta as output. porta=0; //make porta pins low. trisb=0; //make portb as output. portb=0; //make portb pins low. while(1) //endless loop. { unsigned int b0; //declare b0 as unsigned integer variable. unsigned char b1; //declare b1 as unsigned character. for (b0=0;b0<1000;b0++) //for loop to display 0-999. { hundreds=b0/100; //get the hundred's value. tens=(b0%100)/10; //get the ten's value. units=b0%10; //get the unit's value. for (b1=0;b1<100;b1++) //display data hundred times. { portb = get_value(hundreds); //get segment value. porta.0=1; //switch on tranisistor(H). delay_ms(1); //pause 1ms to see data. porta.0=0; //switch off transistor(H). portb=get_value(tens); //get segment value. porta.1=1; //switch on tranisistor(T). delay_ms(1); //pause 1ms to see data. porta.1=0; //switch off transistor(T). portb=get_value(units); //get segment value. porta.2=1; //switch on tranisistor(U). delay_ms(1); //pause 1ms to see. porta.2=0; //switch off transistor(U). } } } } //lookup table for the segment values. unsigned char get_value(unsigned char y) { unsigned char segments[10]={0xDE,0x50,0xE6,0xF4,0x78,0xBC,0xBE,0x54,0xFE,0xFC}; return segments[y]; } /* The C program first intializes the ports as output. Next we enter the while loop where we have created a for loop which will make b0 to take values from 0-999. Next we proceed to determine the hundred's, ten's and unit's value of b0. These values are then passed on to the lookup table which is an array of values. The array contains the segment value that has to be outputted on portb. The values are for "0" to "9" decimal.When this value is outputted on portb, the appropriate leds light up to from the digits. First we output the hundred's value on portb & switch on the transistor(H) connected to the hundreds digit for a brief period of 1ms and then switch it off. Next we place the ten's value on portb and switch on & off the transistor(T) & then we do the same thing for the units value. This is repeated for 100 times so that we can see all the three digits on all the time. Changing the loop value b1 will change the rate at which numbers will appear on the display. You can vary the delay time as well as the loop value b1 to see the effect.See how an array is declare in the function get_value & how the return statement returns the appropriate segment value.All variables are declared as unsigned character in order to save program words.This program compiles to 208 words. */ pic13.bmp pic11modified.bmp
22. ## "pic Micro Programming In Boostc For Beginners"

/* BINARY CODED DECIMAL DISPLAY In this project we will display the binary data outputted by the port on two seven segment LED modules. We will make the seven segment display decimal 0 to 99. We can use the 4511 BCD to 7-segment converter IC for our purpose. The lower nibble of portb is connected to one of the 7-seg- ment via 4511 to display the units value & the higher nibble of portb is connected to the second 7-segment via another 4511 to display the tens value. Source Boost IDE has got this plugin on its simulator. So its very easy to verify your code. Connections: pin4- MCLR pin to +5v via 4.7K resistor. pin15-16 4mhz crystal(across pin 15 & 16). connect 22pf capacitors( between pin15 & gnd, pin16 & gnd). lower nibble or units display. pin6-RB0-connect to pin7 of 4511(1). pin7-RB1-connect to pin1 of 4511(2). pin8-RB2-connect to pin2 of 4511(4). pin9-RB3-connect to pin6 of 4511(8). upper nibble or tens display. pin10-RB4-connect to pin7 of 4511. pin11-RB5-connect to pin1 of 4511. pin12-RB6-connect to pin2 of 4511. pin13-RB7-connect to pin6 of 4511. Remaining pin connections of 4511. pin13-to "A" segment of 7-segment via 470 ohms. pin12-to "B" segment of 7-segment via 470 ohms. pin11-to "C" segment of 7-segment via 470 ohms. pin10-to "D" segment of 7-segment via 470 ohms. pin9- to "E" segment of 7-segment via 470 ohms. pin15-to "F" segment of 7-segment via 470 ohms. pin14-to "G" segment of 7-segment via 470 ohms. pin16-to +5v. pin3-to +5v. pin4-to +5v. pin5-to ground. pin8-to ground. 7-segment display-common cathode-to ground. */ #include <system.h> #pragma CLOCK_FREQ 4000000 // config clock to 4mhz. // Set configuration fuse. #pragma DATA _CONFIG, _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF void main() { trisb=0; //make portb as output. portb=0; //set portb pins to low. char b0; //declare b0 as character type variable. char units; //declare units as character type variable. char tens; //declare tens as character type variable. char bcd; //declare bcd as character type variable. while(1) //endless loop { for(b0=0;b0<100;b0++) //for loop, which outputs 0-99 as BCD. { tens=b0/10; //tens value is first calculated. units=b0%10; //next units value is derived. tens *= 16; //we shift the tens value to the upper nibble. bcd=units+tens; //we add the upper & lower nibble to get BCD value. portb=bcd; //out the BCD value on portb. delay_ms(250); //wait half a second for us to see display. delay_ms(250); } } } /* This program compiles to 91 words. In the above program change all char variables to unsigned char variables and see the program words saved. */
23. ## "pic Micro Programming In Boostc For Beginners"

/* BUTTON TEST In this program we make our microcontroller(PIC16F84) to read switches connected to porta which will dynamically modify the program as it runs. We have connected three switches to the porta. We have named them button1, button2, button3. The main program is the scrolling LED program we had seen earlier. Pressing button1 will change the direction in which the LEDs scroll. Pressing butt- on2 will increment the delay time for which the LEDs are lit. pres- sing button3 will reduce the delay time for which the LEDs are lit. Before you load the hex file onto your chip, you can use the built in simulator on the SourceBoost IDE to check if your code is working just as intended.Read the IDE user's manual to get help to configure your plugins. Connections: pin4- MCLR pin to +5v via 4.7K resistor pin15-16 4mhz crystal(across pin 15 & 16) connect 22pf capacitors( between pin15 & gnd, pin16 & gnd) pin6-RB0-connect LED to pin6 via 470 ohms. Cathode of LED to gnd. pin7-RB1-connect LED to pin7 via 470 ohms. Cathode of LED to gnd. pin8-RB2-connect LED to pin8 via 470 ohms. Cathode of LED to gnd. pin9-RB3-connect LED to pin9 via 470 ohms. Cathode of LED to gnd. pin10-RB4-connect LED to pin10 via 470 ohms. Cathode of LED to gnd. pin11-RB5-connect LED to pin11 via 470 ohms. Cathode of LED to gnd. pin12-RB6-connect LED to pin12 via 470 ohms. Cathode of LED to gnd. pin13-RB7-connect LED to pin13 via 470 ohms. Cathode of LED to gnd. pin17-RA0-connect switch, button1. pin18-RA1-connect switch, button2. pin1-RA2-connect switch, button3. There are two different ways in which you can configure your switch. .ie. the switch can output a high when pressed or a low when pressed. Based on this information you need to modify the code in your if statement.But this code works as it is on the simulator. In the future i will provide the schematic for all the projects done here. If you hard wire this project, make sure your micro sees the right switch state. */ #include <system.h> #pragma CLOCK_FREQ 4000000 // Set configuration fuse #pragma DATA _CONFIG, _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF void check1(void); // declare that you are using a routine // called check1. void check2(void); // declare that you are using a routine // called check2. int duration; // duration is declared as global variable. #define button1 porta.0 // button1 is alais for porta.0 #define button2 porta.1 // button2 is alais for porta.1 #define button3 porta.2 // button3 is alais for porta.2 void main(void) { // Program entry point. trisa=7; // set porta pin0-pin2 as inputs. trisb=0; // set portb pins as output. portb=0; // set portb pins to low. duration=250; // set initial value of delay at 250ms while(1) // infinite loop. { if(button2) // if button2 is pressed, execute { // the following statements. duration+=10; // increment duration by 10ms. if(duration==1000) // if duration equals 1000ms, { duration=990; // make it 990ms. } // this will limit the max delay. } if(button3) // if button3 is pressed, execute { // the following statements. duration-=10; // reduce delay by 10ms. if(duration==10) // if delay equals 10ms, { duration=20; // make it 20ms. } // this will limit the min delay. } if(button1==0) // if button1 pin is not pressed, { // execute the statements below check1(); // call routine check1. portb <<= 1; // shift bits in portb to its left, ones. delay_ms(duration); // pause for the value of duration. } else // else if porta.0 pin is high { // execute the statements below. check2(); // call routine check2. portb>>=1; // shift bits in portb to its right by one place. delay_ms(duration); // pause for the value of duration. } } } void check1(void) // check1 routine. { if(portb==0) // if portb equals zero, { // then execute the statements below. portb=1; // make portb to display 1. delay_ms(duration); // pause for the value of duration. } } void check2(void) // check2 routine. { if(portb==0) // if portb equals zero, { // then execute the following statements. portb=128; // make portb to display 128. delay_ms(duration); // pause for the value of duration. } } /* Lets try to understand the C program line by line. In the beginning of the program you can see two routines check1 & check2. These routines get called from the main routine void main(void). In Basic programming we call it sub-routines. Unlike the Basic sub-routine it does not have a return statement.It is important that you declare all the routines used at the beginning of the code for the compiler to recognize it latter in the program. The routine check1 checks if portb value is zero and if found true makes it equal to one, so that the binay progression can repeatedly continue. Similarly check2 checks if portb valve is zero and if true makes it equal to 128, so that the binary regression can continue. See that check2 rou- tine is called only if button1 is pressed or true. Next in line is int duration; statement. See that duration is declared as an integer because we require more than one byte to store a duration of 1000. Also you can see that it is declared outside the all the routines, meaning that all routines can access the variable and modify it. Thats why we call it global variable. The next statement is #define button1 porta.0. What the compiler does is, it simply replaces button1 with porta.0 while compiling. For us its more convenient and easy to understand the code.So the name button1 is an alais for porta.0. The rest of the program is self explanatory.In the main routine trisa=7; sets porta pins 0-2 as input. trisb=0 makes all pins in portb as output. The delay variable is given an initial value of 250ms. Then the program enters the endless loop, while(1);. In this loop we check if the buttons are pressed. The if statement if(button2) is same as if(button2==1). It means if button2 is true or 1 execute the statements that follows. One other new operator we have used is portb>>=0; and portb<<=0;. They are shift right & assign and shift left and assign operators. They create the same effect as b0*=2; and b0/=2; used in our earlier program. This program compiles to 98 words. */ PIC8modified.bmp
24. ## "pic Micro Programming In Boostc For Beginners"

/* BINARY PROGRESSION The binary progression program lights each LED in sequence, starting from the first LED connected to RB0, then the second led(RB1) while the first is switched off and then the third led connected to RB2 is switched on while the second led is switched off and so on. So the LEDs will scroll from left to right. Before you load the hex file onto your chip, you can use the built in simulator on the SourceBoost IDE to check if your code is working just as intended. Read the IDE user's manual to get help to configure your plugins. Connections: pin4- MCLR pin to +5v via 4.7K resistor pin15-16 4mhz crystal(across pin 15 & 16) connect 22pf capacitors( between pin15 & gnd, pin16 & gnd) pin6-RB0-connect LED to pin6 via 470 ohms. Cathode of LED to gnd. pin7-RB1-connect LED to pin7 via 470 ohms. Cathode of LED to gnd. pin8-RB2-connect LED to pin8 via 470 ohms. Cathode of LED to gnd. pin9-RB3-connect LED to pin9 via 470 ohms. Cathode of LED to gnd. pin10-RB4-connect LED to pin10 via 470 ohms. Cathode of LED to gnd. pin11-RB5-connect LED to pin11 via 470 ohms. Cathode of LED to gnd. pin12-RB6-connect LED to pin12 via 470 ohms. Cathode of LED to gnd. pin13-RB7-connect LED to pin13 via 470 ohms. Cathode of LED to gnd. */ #include <system.h> #pragma CLOCK_FREQ 4000000 // config clock to 4mhz. // Set configuration fuse #pragma DATA _CONFIG, _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF void main() { trisb=0; // make portb pins as output char b0; // declare b0 as character char b1; // declare b1 as character while(1) // endless loop { b0=1; // initialize b0 variable to 1. portb=b0; // display b0 value on portb delay_ms(250); // pause 0.5s for us to see the lit LED delay_ms(250); for(b1=0;b1<7;b1++) // for loop for seven times { b0*=2; // C syntax for multiply and assign. portb=b0; // similar to basic syntax of b0=b0*2 delay_ms(250); // pause 0.5 sec to see the LED on. delay_ms(250); } } } /* The only statement new in this C program is " b0*=2;". Here the C compiler multiplies b0 value with 2 and assigns this new value to b0. It is similar to b0=b0*2. Multiplying b0 with 2, causes the bits in b0 to shift left by one place. Take a print of BoostC compiler reference manual and you will find the list of arithmatic operators, assignment operator, comparison operator, logical operators. This program compiles to 43 words. In the above code change b0=1; as b0=128; in the while loop and also change b0 *= 2; as b0 /= 2; in the for loop. Build the code and check it on the simulator. What do you see? You can see the LED patern has reversed, ie. the LEDs would scroll from left to right. You could call it regression. If you wish to store it, use the save as option first and then build it using the quick buid option in the project menu of the tool bar. */
25. ## Is The Simulator Precise?

Hi guys, I was just testing the code for my thread " pic micro programming in BoostC for beginners ", using the simulator. I am using the porta pins to read buttons and dynamically change the delay time. portb is used as output connected to LEDs. The LEDs are made to scroll from left to right or right to left based on the position of switch in porta.0 Now the question. When i run it on the simulator, the vaiable watch increments or decrements the duration variable correctly but the LEDs in the LED block suddenly moves fast or slow not commensurate with the delay(duration) variable. Some time it works fine? Can any one say why? See code below. #include <system.h> #pragma CLOCK_FREQ 4000000 // Set configuration fuse #pragma DATA _CONFIG, _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF void check1(void); void check2(void); int duration; // duration is declared as global variable #define button1 porta.0 // button1 is alais for porta.0 #define button2 porta.1 // button2 is alais for porta.1 #define button3 porta.2 // button3 is alais for porta.2 void main(void) { trisa=7; // set porta pin0-pin3 as inputs. trisb=0; // set portb pins as output. portb=0; // set portb pins to low. duration=250; while(1) // infinite loop. { if(button2) // if button2 is pressed, execute { // the following statements. duration+=10; // increment duration by 10ms. if(duration==1000) // if duration equals 1000ms, { duration=990; // make it 990ms. } } if(button3) // if button3 is pressed, execute { // the following statements. duration-=10; // reduce delay by 10ms. if(duration==10) // if delay equals 10ms, { duration=20; // make it 20ms. } } if(button1==0) // if button1 pin is not pressed, { // execute the statements below check1(); // call routine check1. portb <<= 1; // shift bits in portb to its left, ones. delay_ms(duration); // pause for 0.5 seconds. } else // else if porta.0 pin is high { // execute the statements below. check2(); // call routine check2. portb>>=1; // shift bits in portb to its right by one place. delay_ms(duration); // pause 0.5 seconds. } } } void check1(void) // check1 routine. { if(portb==0) // if portb equals zero, { // then execute the statements below. portb=1; // make portb to display 1. delay_ms(duration); // pause 0.5 seconds. } } void check2(void) // check2 routine. { if(portb==0) // if portb equals zero, { // then execute the following statements. portb=128; // make portb to display 128. delay_ms(duration); // pause 0.5 seconds. } } Raghunathan
×