RS232 motor control using ADC

Status
Not open for further replies.

varunme

Advanced Member level 3
Joined
Aug 10, 2011
Messages
741
Helped
17
Reputation
36
Reaction score
17
Trophy points
1,318
Activity points
5,765
The following code does not works ,

i want to control a stepper motor with the values send by rs232 and get the feedback from the pot, but to check the values of adc and uart , i just used dummy values in the below code

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
unsigned char ch;                    //
unsigned int adc_rd;                 // Declare variables
char *text;                          //
long tlong,ulong;
char output[7];
int i,rot; 
float ufloat,tfloat;
int uint;
 
 
void main() {
CMCON |= 7;
 
ADCON1 = 0X04;
ADCON1 = 0;
TRISA = 0x04;
ADCON1 = 0x80;
TRISB =0;                       //
PORTB=0x00;
 
 
while (1) {
 
 
        adc_rd = ADC_Read(2);        // A/D conversion. Pin RA2 is an input.
        Lcd_Out(2,1,text);           // Write result in the second line
        tlong = (long)adc_rd * 5000; // Convert the result in millivolts
        tlong = tlong / 1023;        // 0..1023 -> 0-5000mV
        ch = tlong / 1000;           // Extract volts (thousands of millivolts)
                                     // from result
 
 
          UART1_Init(9600);               // Initialize UART1 module at 9600 bps
          Delay_ms(100);                  // Wait for UART 1module to stabilize
          UART1_Write_Text("Start");
          UART1_Write(13);
          UART1_Write(10);
 
 
                 
                 
         while (!UART1_Data_Ready());        // wait for UART character in buffer
 
        // Initialize string with all '\0'
        for(i=0; i<7; i++){
            output[i] = '\0';
        }
 
        UART1_Read_Text(output, "m", 255);   // read input from uart
      
        ulong = atol(output);
        
        
                    while(1)      {
                    
 
           if ( ulong =! tlong )
             {
 
                          if (ulong > tlong )
                          { PORTB.f1=1;
                          UART1_Write_Text("1");
                          break;
                          }
                          else //(ulong < tlong )
                         {PORTB.f1=0;
                         PORTB.f2=1;
                         UART1_Write_Text("2");
                         break;
                         }
 
 
 
           }
            if ( ulong == tlong )
            {
            PORTB.f3=1;
            UART1_Write_Text("3");
            PORTB.f1=0;
            PORTB.f2=1;
            break;
            }
 
          }
          }
 }

 

Are you using mikroC. If yes, then where is your LCD Initialization, UART Initialization etc., Can you post your schematic. What PIC are you using? What Fosc?
 

I left it to make the code smaller to post it here
 

Is it
Code:
 while(1)      {
                    
 
           if ( ulong =! tlong )
             {

or

Code:
while(1)      {
                    
 
           if ( ulong != tlong )
             {

why there is 2 while(1) loops?
 
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
yes, i didnt noticed it , thank you , i will check by removing it
I should check all inside the UART ready loop isnt it ?

edit : sorry, one while loop and the UART ready loop
 

where is
Code:
 char *text;
used in the code? Why isn't it initialized like
Code:
 char *text = "0000000"

The
Code:
 for(i=0; i<7; i++){
            output[i] = '\0';
        }

makes the length of output variable to 0, because you are writing \0 to output[0]. How can you use such variable?
 
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…