#include <mega8535.h> 
#include <delay.h>

#define clk     DDRA.0
#define dio     DDRA.1
#define cs      DDRA.2  

#define diopin  PINA.1 

#define clkx     PORTA.0
#define diox     PORTA.1
#define csx      PORTA.2  

#define OUTPUT  1
#define INPUT   0
#define LOW     0
#define HIGH    1

// Standard Input/Output functions
#include <stdio.h>   

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>

// Declare your global variables here 

int buffx[33],buffy[33];

int rvCount[12];
int MSBCount=0;
int b,i;
int vCount;
int axCount;
int ayCount;

int axdata;
int aydata;

int count;
int frac;
float mVolts;
float gForce;    

int readdata(int set_D1,int set_D0)
{
  
  dio=OUTPUT;
  diox=LOW;
  csx=LOW; 
  clkx=LOW;      
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  
  // start bit
  clkx=HIGH; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  clkx=LOW; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  diox=HIGH; 
  clkx=HIGH; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  clkx=LOW; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  diox=LOW; 
  
  //control bit: single
  diox=HIGH;
  clkx=HIGH; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  diox=LOW; 
  clkx=LOW; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  
   //control bit: d2
  clkx=HIGH; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  clkx=LOW; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  
  //control bit: d1
  if (set_D1==0)
    diox=LOW;
  if (set_D1==1) 
    diox=HIGH;
    
  clkx=HIGH; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  clkx=LOW; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  diox=LOW;
  
  //control bit: d0
  if (set_D0==0)
    diox=LOW;
  if (set_D0==1) 
    diox=HIGH;     
    
  clkx=HIGH; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  clkx=LOW; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  diox=LOW;
  
   //delay bit
  clkx=HIGH; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  clkx=LOW; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
 
   dio=INPUT;
  
  //data bits
 for (i=0; i<=11; i++) 
 {
  clkx=HIGH;
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  clkx=LOW; 
  rvCount[i] = diopin; 
  #asm ("cli");
  #asm ("nop");
  #asm ("nop");
  #asm ("sei");
  }
 
 MSBCount = 0;
 
 b=2048;   
 
 for (i = 0; i <= 11; i++)
  {  
      MSBCount += (rvCount[i]*b);
      b=b/2;
  }
  
csx=HIGH;
 
  return MSBCount; 
  
 }

void mulai()
{ 
 
  vCount = readdata(1,1);
  axCount = readdata(0,0);
  ayCount = readdata(0,1);
  
  axdata=2048-axCount;
  aydata=2048-ayCount;
    
  printf(" X_CH0: ");  
  printf("%i\n\r",axdata);   
  printf(" Y_CH1: ");
  printf("%i\n\r",aydata);

printf(" g-forces:\n\r");

for (i=1; i<=3; i++) {
  
  switch (i) 
  {
    case 1:
      count = axCount;
      break;
    case 2:
     count = ayCount;
      break;
  }
  
  if (count >= vCount)
  {
        gForce = (((long)count) - ((long)vCount))*0.0022;
  }
  else
  {
  gForce = -(((long)vCount) - ((long)count)) * 0.0022;
  } 
  printf(" ");
  printf("%i",(int)gForce);
  printf(".");
  
  if (gForce >= 0)
          frac = (gForce - ((int)gForce)) * 1000;
  else
          frac = (((int)gForce)- gForce) * 1000;
  
  printf("%d\n\r",frac) ;
 }    
 
 //printf("\n\r");
  
  mVolts = vCount*0.00080586;
  
  printf(" VRef Volts: ");
  printf("%i",(int)mVolts);
  printf(".");
  
  if (mVolts >= 0)
          frac = (mVolts - ((int)mVolts)) * 1000;
  else
          frac = (((int)mVolts)- mVolts ) * 1000;

  printf("%d\n\n\r",frac) ; 

  delay_ms(300);
}      

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=Out 
// State7=T State6=T State5=T State4=T State3=T State2=1 State1=0 State0=0 
PORTA=0x04;
DDRA=0x07;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x4D;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;       

// LCD module initialization
lcd_init(16);

cs=OUTPUT;
clk=OUTPUT;
dio=OUTPUT;
csx=HIGH; 
  
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("  Memsic2125  ");
lcd_gotoxy(0,1);
lcd_putsf("2-Axis  Accelero");   

delay_ms(1500);

while (1)
      {
      // Place your code here  
      mulai();  
      lcd_clear();
      lcd_gotoxy(0,0);
      lcd_putsf(" X     Y ");
      lcd_gotoxy(0,1);
      sprintf(buffx,"%i",axdata);
      lcd_puts(buffx);  
      sprintf(buffy,"%5i",aydata);
      lcd_puts(buffy);
    
      
      };
}

Can you help me how to fix this program? ^_^

When I try to compile it, it would give me the warning
--> Suspicious pointer in function main.


Thanks before......

On what line? We can't compile this on a standard C-compiler.
Did you set the printf()-support to "full"?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.