BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Fri Jul 20, 2018 1:45 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Demo Code For Both Nano28/40 UPDATE 081910
PostPosted: Tue Aug 25, 2009 8:26 pm 
Offline
Master

Joined: Mon Aug 18, 2008 1:26 am
Posts: 799
Location: CA bay Area
Code:
 ; Nano28_40 version. This will NOT work with a Nano18!
;;; 081910 - EDIT had to update LCD pinouts syntax, added servos
; Wiring: (If you are using a Nano Development Board, refer to data sheet diagram)
; P0, P1, P4 to P7 - LCD
; P3 - LCD Backlight
; P8 - Servo 1
; P9 - Servo 2
; P10 - SPK1 (speaker), install 1uF - 10uF capacitor between P10 and speaker!
; P11 - SW2 (button1)
; P12 - SW3 (Button2)
; P13 - SW4 (Button3)
; P14 - LED1
; P15 - LED2
; P30 - POT2 (for Nano40), P17 for Nano28
; P31 - POT1 (for Nano40), P18 for Nano28

;Setup 32 bit variable
audio var long
temp var long
temp2 var long
cnt var long
cnt=0
; Set floating point variable
volume var float
oldvolume var float
; Set Servo position values
SERV1POS var word
SERV2POS var word
SERV1POS = 1800  ; 1800 seemed a safe value to start at, your position values may be different
SERV2POS = 1800

; Setup LCD
lcdinit p0\p1\p7\p6\p5\p4
lcdwrite p0\p1\p7\p6\p5\p4,[CLEARLCD,HOMELCD,SCR,TWOLINE,"Hello World!"]
PAUSE 500
lcdwrite p0\p1\p7\p6\p5\p4,[CLEARLCD,HOMELCD, "POT1   POT2"]
;Set pins to known state
low p15
low p14
high p3  ; turns on LCD backlight, not available on Nano Dev Board

; Set servos at HOME position
SERVO 8, 0
SERVO 9, 0

;Enter main program. Read adin pins, load values into variables
main
IF cnt = 0 then
SEROUT s_out, i9600, [0]  ; clear terminal display
ENDIF

; Set servos full CW
SERVO 8, SERV1POS
SERVO 9, SERV2POS
cnt=cnt+1
adin P18, temp  ; use P31 for Nano40, P18 for Nano28 (controls frequency at speaker)
output P3
adin P17, temp2  ; use P30 for Nano40, P17 for Nano28 (controls speaker volume, LCD contrast)

; Set volume level of speaker based on potientometer value
volume = 1.0 - (TOFLOAT(temp2)/1024.0)
; Put tone out on speaker, frequency and volume based on pot1 and -2 settings
if(TOINT(oldvolume<>volume) OR temp<>audio)then
   audio = 1023 - temp   ; As temp rises and falls, so does the frequency
   hpwm 0,audio*16,TOINT(TOFLOAT(audio*8)*volume)
   oldvolume = volume
endif

;Write value to LCD
lcdwrite p0\p1\p7\p6\p5\p4,[SCRRAM+40,dec4 temp\4,"   ",dec4 temp2\4]
;Send analog  and volume values to serial port
serout S_OUT,i9600,["POT1: ", dec temp,"  POT2: ",dec temp2,"  VOLUME: ",real volume, "  CNT: ", dec CNT, 13]
;Button switch state is loaded to LED (output pin).
out15=in13  ; SW4 controls LED1
out14=in12  ; SW3 controls LED2
out3=in11  ; press SW2, turn off LCD backlight


; Set servos full CCW
SERVO 8, -(SERV1POS)
SERVO 9, -(SERV2POS)

goto main

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: Demo Code For Both Nano28/40
PostPosted: Mon Feb 01, 2010 7:34 pm 
Offline
Citizen

Joined: Sat Jul 04, 2009 3:09 am
Posts: 11
I have a question about the following line,

adin P18, temp ; use P31 for Nano40, P18 for Nano28 (controls frequency at speaker)

For the nano40, does it have to be P31 or can it be any other pin with an A/D channel?

Thanks!!!


Top
 Profile  
 
 Post subject: Re: Demo Code For Both Nano28/40
PostPosted: Mon Feb 01, 2010 9:02 pm 
Offline
Master

Joined: Mon Aug 18, 2008 1:26 am
Posts: 799
Location: CA bay Area
Hello Andy. I haven't tested this code using any pins different from what's stated, but I see no reason why the user can't use another AD pin of their choosing. Go ahead! :D Go wild! Try another pin! But do tell us if a simple pin swap creates a problem! :o Bug reports are always welcome here.
Have a nice project.

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: Demo Code For Both Nano28/40 UPDATE 081910
PostPosted: Fri Aug 20, 2010 12:17 am 
Offline
Master

Joined: Mon Aug 18, 2008 1:26 am
Posts: 799
Location: CA bay Area
I discovered glitches in the code. Some were due to syntax changes by BasicMicro. Some were just outright bloopers. No one mentioned these?!
I had to update the pinouts for LCD use, that's the syntax change.
I added SERVO commands to drive two servo motors. This greatly slowed down the time it takes to run the main loop. So when you change a pot setting, you'll be waiting a few seconds to see or hear differences in the displays and speaker.
I finally used the 32-bit CNT variable in the code. It increments in every loop of the main routine. This counts up to 4,294,967,295, and can be seen in the terminal screen.
I cleaned up the comments some, also.

_________________
kenjj
http://blog.basicmicro.com/
http://kjennejohn.wordpress.com/


Top
 Profile  
 
 Post subject: Re: Demo Code For Both Nano28/40 UPDATE 081910
PostPosted: Fri Aug 20, 2010 4:09 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
A suggestion when using the servo command in a loop, you can specify 0 for the number of cycles. When set to 0 the servo command ONLY outputs the pulse part(will always take 3ms even though the pulse may be less) and then immeediately returns. So if you want to controll up to 6 servos in the 20ms window you can or if you want to controll 2(like in your case) you will have approx 14ms left over for other stuff to happen. You need to make sure you add enough delay in your main loop so that the servo pulses are output approx every 20ms. Some servos are more sensitive to this window than others.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO