Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Help for programming USART

Status
Not open for further replies.

a4arijit

Newbie level 6
Joined
May 16, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
India
Activity points
1,485
Hi,
Once again I'm back with my stupidity..I'm working on a project that process an ADC data and transmit them to the PC using a DB9 COM port.For that I need the USART working.For testing purposes I'm using HyperTerminal 7 .I came up with a program (its worse enough to laugh) but its highly predictable that I failed..Can anyone put me out of my misery??I'm posting the code below..Please help me..

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/************************************************** ***
This program was produced by the
CodeWizardAVR V2.03.4 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
HP InfoTech, Development Tools for Microcontrollers, C Compilers, In-System Programmers
 
Project :
Version :
Date : 6/27/2011
Author :
Company :
Comments:
 
 
Chip type : ATmega16
Program type : Application
Clock frequency : 16.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
************************************************** ***/
 
#include <mega16.h>
 
// Standard Input/Output functions
#include <stdio.h>
 
#include <delay.h>
#include <math.h>
 
#define ADC_VREF_TYPE 0x00
 
 
float temperature;
float temperaturee;
unsigned int temporary;
 
 
void updatetemp(void);
void sett_regs(void);
unsigned int read_adc(unsigned char adc_input);
 
// Main function
void main(void) {
//--------------- Setting IO and peripheral at startup!
    sett_regs();
 
//--------------- Time for going CRAZY (looping forever)..
    for(;;) { //mainloop looping forever until ERROR/POWER SHUTDOWN/RST/WDT ACTIVE
#asm("wdr") //kick your DOG before DEAD! hwahwa... :D
        updatetemp();
#asm("nop")
    };
}
 
void updatetemp(void) {
    if (temporary != read_adc(1))
    {
        temperature = read_adc(1)/2.007;
        temperaturee = ((9*temperature)/5)+32;
        temporary = read_adc(1);
        delay_ms(100);
    }
}
 
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
    ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
    delay_us(10);
// Start the AD conversion
    ADCSRA|=0x40;
// Wait for the AD conversion to complete
    while ((ADCSRA & 0x10)==0);
    ADCSRA|=0x10;
    return ADCW;
}
 
// Declare your global variables here
 
void sett_regs(void)
 
{
 
 
// Declare your local variables here
 
// Input/Output Ports initialization
// Port A 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
    PORTA=0x00;
    DDRA=0x00;
 
// 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: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
    UCSRA=0x00;
    UCSRB=0x18;
    UCSRC=0x86;
    UBRRH=0x00;
    UBRRL=0x67;
 
// Watchdog Timer initialization
// Watchdog Timer Prescaler: OSC/2048k
#pragma optsize-
    WDTCR=0x1F;
    WDTCR=0x0F;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
 
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;
 
// ADC initialization
// ADC Clock frequency: 250.000 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: None
    ADMUX=ADC_VREF_TYPE & 0xff;
    ADCSRA=0x86;
 
    while(1)
    {
        temperature =getchar();
        putchar (temperature);
    }
}

 
Last edited:

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,020
Reputation
4,156
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
I have used the syntax highlighter so that I can explain to you with specific line numbers.

In line 46 you call sett_regs(); it executes but it never returns to main because you have the while loop in line 203, you can never get out of it and you never get an ADC result.
The only thing you do is:
temperature =getchar();
putchar (temperature);

Your code can't reach the for loop in line 49

Alex
 

a4arijit

Newbie level 6
Joined
May 16, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
India
Activity points
1,485
I have used the syntax highlighter so that I can explain to you with specific line numbers.

In line 46 you call sett_regs(); it executes but it never returns to main because you have the while loop in line 203, you can never get out of it and you never get an ADC result.
The only thing you do is:
temperature =getchar();
putchar (temperature);

Your code can't reach the for loop in line 49

Alex
Can you kindly name a function that calls on a "while" loop and also sets up the registry???
 

a4arijit

Newbie level 6
Joined
May 16, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
India
Activity points
1,485
I don't understand what you mean.

Alex


I mean I've tried to print the data "temperature" which contains the ADC result with "printf" inside the "void main(void)" but its not showing any result..whereas I printed a string on Termite 2.6 to test the USART using printf and it was a success..

---------- Post added at 23:18 ---------- Previous post was at 23:12 ----------

I'll post an updated code for more clarification..
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,020
Reputation
4,156
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
temperature is a float variable so how do you expect putchar (temperature); to work?
putchar can be used with a char

Alex
 

a4arijit

Newbie level 6
Joined
May 16, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
India
Activity points
1,485
temperature is a float variable so how do you expect putchar (temperature); to work?
putchar can be used with a char

Alex

Actually using the getchar and putchar was stupid..I used printf("%0.1f",temperature)..but still its not printing any data..I used the functions printf and while inside "void main(void)"..
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,020
Reputation
4,156
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
Something as simple as that works fine for me


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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
Chip type               : ATmega16
Program type            : Application
AVR Core Clock frequency: 16,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/
 
#include <mega16.h>
 
// Standard Input/Output functions
#include <stdio.h>
 
// Declare your global variables here
 
void main(void)
{
// Declare your local variables here
float temperature;
// Input/Output Ports initialization
// Port A 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 
PORTA=0x00;
DDRA=0x00;
 
// 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: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x67;
 
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
 
temperature=1.23;           
printf("%0.4f",temperature); // this is transmitted through the uart
 
while (1)
      {
      // Place your code here
       
      };
}



Alex
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top