![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
adrian
Joined: 08 Sep 2003 Posts: 92 Location: Glasgow, UK
|
problematic array |
Posted: Fri Jun 25, 2004 4:08 pm |
|
|
I have a question on how to increment an array.
First the important stuff:
PCM 3.148
12F629
I have an array which I initialise thus:
Code: |
// all variables are global \\
//numbers
#define ZERO 0x7E
#define ONE 0x0C
#define TWO 0xB6
#define THREE 0x9E
#define FOUR 0xCC
#define FIVE 0xDA
#define SIX 0xFA
#define SEVEN 0x0E
#define EIGHT 0xFE
#define NINE 0xDE
//LCD display
int digits[10] = {ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE};
int digit_counter = 0; //used to keep track of displayed digit
int bit_counter = 0; //used to keep track of bits within each digit
|
It is used in the main loop
Code: |
while (TRUE)
{
//cycle through digits 0 -> 9
for (; digit_counter <=9; digit_counter++)
{
//increment array
digits[digit_counter];
//set clock low
output_low(S_CLK);
//clock bits out from the array element
for (; bit_counter <=7; bit_counter++)
{
output_bit(S_DATA,shift_right(digits,1,0));
//clock bit out
output_high(S_CLK);
delay_cycles(1);
output_low(S_CLK);
}
//reset bit_counter
if (8 == bit_counter)
{
bit_counter = 0;
}
}
}
|
What I am trying to achieve, is to clock each element of the array serially out through a port pin. It will work the first time as 'digit_counter' is initialised at 0, and I can see the code running as I expect it to via the simulator. However I cannot get the next element of the array to load. I was hoping to use 'bit_counter' to index the elements of the array?
The code compiles and when I run it through the simulator it steps over the line.... digits[digit_counter]; which I hoped was going to increment the array?
Looking at the .lst there is no code shown for this line?
Its getting late and I've probably done something silly - its just that I cant see it. All suggestions gratefully received. |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Jun 25, 2004 5:05 pm |
|
|
Code: | //increment array
digits[digit_counter];
|
This line is doing nothing, you are reading a value but don't do anything with it. Your compiler has optimized it away, that's why your simulator is stepping over it.
One possible solution is to change this line to
Code: | new_digit = digits[digit_counter];
|
and then also change
Code: | output_bit(S_DATA,shift_right(digits,1,0)); |
to
Code: | output_bit(S_DATA,shift_right(new_digit,1,0)); |
|
|
![](templates/subSilver/images/spacer.gif) |
dvsoft
Joined: 28 Nov 2003 Posts: 46
|
|
Posted: Sat Jun 26, 2004 1:40 am |
|
|
Bonjour,
you can try this code
//----------------------------------------------------------------------------
// SPI I/O
//
#ifndef __SPI_IO__
#USE fast_io(c)
#define SPI_SO PIN_A5
#define SPI_SI PIN_A4
#define SPI_SCK PIN_E0
#endif // __SPI_IO__
//----------------------------------------------------------------------------
// Your DATA
//
#define ZERO 0x7E
#define ONE 0x0C
#define TWO 0xB6
#define THREE 0x9E
#define FOUR 0xCC
#define FIVE 0xDA
#define SIX 0xFA
#define SEVEN 0x0E
#define EIGHT 0xFE
#define NINE 0xDE
//LCD display
int digits[10] = {ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE};
//----------------------------------------------------------------------------
// public :
// Function :Write_SPI
// Version :0.1
// Author :Alain
//
// Usage :write_SPI(Data)
//
// Arguments:BYTE Data Data to write
//
// Return :void
//
// Modify :void
//
// Shifts out on the falling edge of the clock.
//----------------------------------------------------------------------------
void write_SPI(BYTE Shift)
{
BYTE BitCnt = 8;
//--- Shifts out
do {
//--- Clock low
output_low(SPI_SCK);
//--- Bit = 1
if(Shift & 0x80)
output_high(SPI_SO);
//--- Bit = 0
else
output_low(SPI_SO);
Shift <<= 1;
//--- Clock high
output_high(SPI_SCK);
} while (--BitCnt);
}
#ifdef __TEST__
//----------------------------------------------------------------------------
void main(void)
{
int8 ByteCnt;
//--- Loop Test 1
for (ByteCnt = 0; ByteCnt < 10; ByteCnt++)
//--- Output the Current Byte
Write_SPI(digits[ByteCnt]);
//--- Loop Test2
ByteCnt = 0;
while (ByteCnt < 10)
//--- Output the Current Byte
Write_SPI(digits[ByteCnt++]);
//--- Loop Test3
ByteCnt = 0;
do {
//--- Output the Current Byte
Write_SPI(digits[ByteCnt]);
} while (++ByteCnt < 10);
}
//----------------------------------------------------------------------------
#endif // ___TEST__
bon courrage
Alain |
|
![](templates/subSilver/images/spacer.gif) |
adrian
Joined: 08 Sep 2003 Posts: 92 Location: Glasgow, UK
|
|
Posted: Sat Jun 26, 2004 2:19 am |
|
|
ckielstra wrote: | Code: | //increment array
digits[digit_counter];
|
This line is doing nothing, you are reading a value but don't do anything with it. Your compiler has optimized it away, that's why your simulator is stepping over it.
|
OK that explains it.
I had assumed - wrongly, that as the for loop was being incremented, the array element would also be incremented as I was using the same variable. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|