PDA

View Full Version : Calculating value with Cos and Sin function on pic16f877



grimmjow
- 24th October 2010, 08:52
Hi everyone,
I'm working on f877 and want to display cos or sin value of an angle.
i could not find any examples about cos function but as i read it says that i have to input angle value between 0-255 .
That means i have to input 63 for 90 degrees.
If i calculate for 63, LCD displays Cos = 3 and Sin = 127 .
Or if i do it for 90, it displays Cos = 65460 and Sin = 102 .

I'd appreciate if anyone could help me with these commands with simple explaination.

Thansk for your helps.

MyCodes:

PORTD = 0
TRISD = 0

DEGREE VAR WORD
COSVALUE VAR WORD
SINVALUE VAR WORD

DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTD
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 3
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2

DEGREE = 63
COSVALUE = COS DEGREE
lcdout $fe,1
lcdout #COSVALUE
pause 2000
SINVALUE = SIN DEGREE
lcdout $fe,$c0
lcdout #SINVALUE

end

ScaleRobotics
- 24th October 2010, 12:25
The result of sin and cos are twos complement. This is a bit hard to describe. Here it is displayed:

4878

The sin and cos functions are made for byte variables. If you use word variables, it will throw your numbers WAY off, like you saw in the cos example you shared. You need to change those back to bytes. Other than that, it sounds like you got it, as you are converting degrees to "binary radians" correctly. (degrees * 255/360)

The results -127 to 127 represent -1 to 1. If you do sin of 270 degrees (270*255/360) = 191 ,sin of 191 would displays an answer of 129 on your LCD. That is actually -127 , or -1.

grimmjow
- 24th October 2010, 13:29
thanks scalerobotics,

i understood the logic, RealCosvalue = Cosvalue/127 . Also i work for angles <90 and it's much more easy to apply proramming

you are a life saver. it was making me mad thinking about how that function works.

Have a nice day.


The result of sin and cos are twos complement. This is a bit hard to describe. Here it is displayed:

4878

The sin and cos functions are made for byte variables. If you use word variables, it will throw your numbers WAY off, like you saw in the cos example you shared. You need to change those back to bytes. Other than that, it sounds like you got it, as you are converting degrees to "binary radians" correctly. (degrees * 255/360)

The results -127 to 127 represent -1 to 1. If you do sin of 270 degrees (270*255/360) = 191 ,sin of 191 would displays an answer of 129 on your LCD. That is actually -127 , or -1.