Veketti
Full Member level 3
- Joined
- Sep 14, 2014
- Messages
- 164
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 16
- Activity points
- 1,700
Dear All,
I bought from Dimensionengineering.com their 3D accelerometer to make G-meter. They have in their web page source code which was made for CCS PCWH compiler. I only have MicroC Pro and I've tried to convert this code to work on microC. So far I've managed to change allmost everything but there's something I'm still missing. When I run the program, on display it says 1. -> 2 -> 3 and keeps repeating that. So I guess display update is working correctly but A/D conversion is not. There are few "setup_" lines which I haven't figured out how to make in microC, perhaps those are the reason why it wont work. I've commented all the original lines that didn't work and added the microC counterpart. As this code was made for the 5g accelerometer I need to change the values to match their 3d accelerometer. Also I planned to include all 3 axes and LCD display.
If you could help me to find what am I still missing I'd be more than happy. I'm not sure am I allowed to paste this code here as the original is available from: https://www.dimensionengineering.com/appnotes/Gmeter/
Here's my porting attempt changes to the original code:
Thank you in advance. Your kind help is greatly appreciated.
Edit: forgot to mention that for newbie like me this CCS code is easier to understand as those instructions are verbal instead of cryptic microC. Is it just me or others feel the same?
I bought from Dimensionengineering.com their 3D accelerometer to make G-meter. They have in their web page source code which was made for CCS PCWH compiler. I only have MicroC Pro and I've tried to convert this code to work on microC. So far I've managed to change allmost everything but there's something I'm still missing. When I run the program, on display it says 1. -> 2 -> 3 and keeps repeating that. So I guess display update is working correctly but A/D conversion is not. There are few "setup_" lines which I haven't figured out how to make in microC, perhaps those are the reason why it wont work. I've commented all the original lines that didn't work and added the microC counterpart. As this code was made for the 5g accelerometer I need to change the values to match their 3d accelerometer. Also I planned to include all 3 axes and LCD display.
If you could help me to find what am I still missing I'd be more than happy. I'm not sure am I allowed to paste this code here as the original is available from: https://www.dimensionengineering.com/appnotes/Gmeter/
Here's my porting attempt changes to the original code:
Code:
//Dimension Engineering G meter project
//Written to be compiled on CCS PCWH 3.216
//Do not use for commercial purposes or we will send robots to destroy your homes
//since we don't change the input/output/highZ states
//of the pins at all, using fast IO will save some unnecessary instructions
// #use fast_io(A)
// #use fast_io(B)
#define CALIBRATED_0G 510 //this is the analog value for the zero G point
#define CALIBRATED_SENSITIVITY 19//this number will convert 10bit analog values
//into Gs
//Character data for the 7 seg display. Not all characters get used in this project
#define ONE 0b11011101
#define TWO 0b10101000
#define THREE 0b10011000
#define FOUR 0b11010100
#define FIVE 0b10010010
#define SIX 0b10000010
#define SEVEN 0b11011001
#define EIGHT 0b10000000
#define NINE 0b11010000
#define ZERO 0b10000001
#define DP 0b01111111
#define A 0b11000000
#define B 0b10000110
#define C 0b10100011
#define D 0b10001100
#define E 0b10100010
#define F 0b11100010
#define G 0b10010000
#define BLANK 0b11111111
#define MINUS 0b11111110
//takes an array of three shorts
void update_display(short* tripledigitpointer)
{
short i;
short digitarray[13]={ZERO,ONE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,A,BLANK,C};
short digitmask=0b01000000; //A6 is digit1, A7 is 2, A0 is digit 3
short output;
for(i=0;i<3;i++)
{
// output_a(digitmask);
// output=digitarray[tripledigitpointer[i]];
PORTA = digitmask;
output=digitarray[tripledigitpointer[i]];
if(i==0) output=output & DP; //put the decimal point on digit 1
// output_b(output);
PORTB = output;
delay_ms(3); //lights on most of the time
// output_b(0xff); //then turn them off to avoid blurring from last digit
PORTB = 0xff; //then turn them off to avoid blurring from last digit
delay_us(10);
digitmask=digitmask<<1; //shifts so next digit is lit
if(digitmask==0) digitmask++; //for digit 3
}
}
void main()
{
short digits[3]={1,2,3};
int analogvalue=0;
int oldanalogvalue=0;
signed int temp=0;
signed int Gvalue=0;
short i=0;
// setup_oscillator(OSC_4MHZ);
// setup_adc_ports(ALL_ANALOG); // ANSEL but it this chip doesn't support
// setup_adc(ADC_CLOCK_DIV_64);
// setup_spi(FALSE);
// setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
// setup_timer_1(T1_DISABLED);
// setup_timer_2(T2_DISABLED,0,1);
ADCON0 = 0b10010101; // setting the A/D module. MicroC
// set_tris_a(0b00111110); //AN1-5 are inputs
// set_tris_b(0); // all outputs for 7 seg display
TRISA = 0b00111110; //AN1-5 are inputs
TRISB = 0; // all outputs for 7 seg display
adc_init();
while(1)
{
update_display(digits); //calling the display routine multiple times
update_display(digits); //to slow down how often the data gets updated
update_display(digits);
update_display(digits);
// set_adc_channel(2); //(this is pin 1 on the PIC connected to DE-ACCM's Y output)
delay_us(10);
// analogvalue=read_adc();
analogvalue=ADC_Read(2);
delay_us(10);
// analogvalue=read_adc();
analogvalue+=ADC_Read(2);//take an average of 2 readings to reduce noise
analogvalue = analogvalue / 2;
temp = analogvalue-oldanalogvalue;
if( temp>2 || temp<-2 ) //ignore small changes to make display more readable
{
oldanalogvalue=analogvalue;
//convert analog value into G digits
Gvalue=analogvalue-CALIBRATED_0G;
Gvalue=Gvalue<<5; //left shifting then dividing allows you to perform
Gvalue=Gvalue/CALIBRATED_SENSITIVITY; //more precise multiplication/division
//now convert from the decimal place digits stored in Gvalue
//to the format of an array of 3 integers
i=2;
while(i!=255)
{
digits[i]=(short)(Gvalue%10);
Gvalue=Gvalue/10;
i--;
}
}
}
}
Thank you in advance. Your kind help is greatly appreciated.
Edit: forgot to mention that for newbie like me this CCS code is easier to understand as those instructions are verbal instead of cryptic microC. Is it just me or others feel the same?
Last edited: