ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 8166

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

© MCS Electronics, 1995-2007

See also

1WRESET , 1WREAD , 1WWRITE , 1WIRECOUNT , 1WRESET , 1WSEARCHFIRST , 1WSEARCHNEXT

Example

'-----------------------------------------------------------------------------

---

: 1wire.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demonstrates 1wreset, 1wwrite and 1wread()

'micro

: Mega48

'suited for demo

: yes

'commercial addon needed

: no

' pull-up of 4K7 required to VCC from Portb.2

' DS2401 serial button connected to Portb.2

'-----------------------------------------------------------------------------

---

$regfile = "m48def.dat"

$crystal = 8000000

$hwstack = 32

' default use 32

for the hardware stack

'default use 10

$swstack = 10

for the SW stack

'default use 40

$framesize = 40

for the frame space

Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

'when only bytes are used, use the following lib for smaller code $lib "mcsbyte.lib"

Config 1wire = Portb.0

'use this pin

'On the STK200 jumper B.0 must be inserted

Dim Ar(8) As Byte , A As Byte , I As Byte

Do

Wait 1

'reset the device

1wreset

Print Err

'print error 1 if

error

'read ROM command

1wwrite &H33

For I = 1 To 8

'place into array

Ar(i) = 1wread()

Next

'You could also read 8 bytes a time by unremarking the next line

'and by deleting the for next above

'read 8 bytes

'Ar(1) = 1wread(8)

For I = 1 To 8

'print output

Print Hex(ar(i));

Next

'linefeed

Print

Loop

'NOTE THAT WHEN YOU COMPILE THIS SAMPLE THE CODE WILL RUN TO THIS POINT

page -313-


© MCS Electronics, 1995-2007

'THIS because of the DO LOOP that is never terminated!!!

'New is the possibility to use more than one 1 wire bus

'The following syntax must be used:

For I = 1 To 8

'clear array to

Ar(i) = 0

see that it works

Next

1wreset Pinb , 2

'use this port and

pin for the second device

'note that now the

1wwrite &H33 , 1 , Pinb , 2

number of bytes must be specified!

'1wwrite Ar(1) , 5,pinb,2

'reading is also different

'read 8 bytes from

Ar(1) = 1wread(8 , Pinb , 2)

portB on pin 2

For I = 1 To 8

Print Hex(ar(i));

Next

'you could create a loop with a variable for the bit number !

For I = 0 To 3 'for pin 0-3 1wreset Pinb , I

1wwrite &H33 , 1 , Pinb , I Ar(1) = 1wread(8 , Pinb , I)

For A = 1 To 8 Print Hex(ar(a));

Next

Print Next

End

CONFIG ACI

Action

Configures the Analog Comparator.

Syntax

CONFIG ACI = ON|OFF, COMPARE = ON|OFF, TRIGGER=TOGGLE|RISING|FALLING

Remarks

ACI

Can be switched on or off

COMPARE

Can be on or off.

When switched ON, the TIMER1 in capture mode will trigger on ACI too.

TRIGGER

Specifies which comparator events trigger the analog comparator

interrupts.

See also

NONE

page -314-


© MCS Electronics, 1995-2007

Example

NONE

CONFIG ADC

Action

Configures the A/D converter.

Syntax

CONFIG ADC = single, PRESCALER = AUTO, REFERENCE = opt

Remarks

ADC

Running mode. May be SINGLE or FREE.

PRESCALER A numeric constant for the clock divider. Use AUTO to let the compiler generate the best value depending on the XTAL

REFERENCE Some chips like the M163 have additional reference options.

Value may be OFF , AVCC or INTERNAL. See the data sheets for the different modes.

Some newer micro's support also : INTERNAL_1.1

INTERNAL_2.56

INTERNALEXTCAP

See also

GETADC

ASM

The following ASM is generated(depending on the chip)

In _temp1,ADCSR ; get settings of ADC

Ori _temp1, XXX ; or with settings

Out ADCSR,_temp1 ; write back to ADC register

Example

'-----------------------------------------------------------------------------

---

: adc.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demonstration of GETADC() function for 8535 or

M163 micro

: Mega163

'micro

'suited for demo

: yes

'commercial addon needed

: no

'use in simulator

: possible

' Getadc() will also work for other AVR chips that have an ADC converter '-----------------------------------------------------------------------------

page -315-


© MCS Electronics, 1995-2007

---

' we use the M163

$regfile = "m163def.dat"

$crystal = 4000000

$hwstack = 32

' default use 32

for the hardware stack

'default use 10

$swstack = 10

for the SW stack

'default use 40

$framesize = 40

for the frame space

'configure single mode and auto prescaler setting

'The single mode must be used with the GETADC() function

'The prescaler divides the internal clock by 2,4,8,16,32,64 or 128 'Because the ADC needs a clock from 50-200 KHz

'The AUTO feature, will select the highest clockrate possible Config Adc = Single , Prescaler = Auto

'Now give power to the chip

Start Adc

'With STOP ADC, you can remove the power from the chip 'Stop Adc

Dim W As Word , Channel As Byte

Channel = 0

'now read A/D value from channel 0

Do

W = Getadc(channel)

Print "Channel " ; Channel ; " value " ; W Incr Channel

If Channel > 7 Then Channel = 0

Loop

End

'The new M163 has options for the reference voltage 'For this chip you can use the additional param :

'Config Adc = Single , Prescaler = Auto, Reference = Internal 'The reference param may be :

'OFF

:

AREF,

internal reference turned off

'AVCC

:

AVCC,

with external capacitor at AREF pin

'INTERNAL : Internal 2.56 voltage reference with external capacitor ar AREF pin

'Using the additional param on chip that do not have the internal reference will have no effect.

CONFIG ATEMU

Action

Configures the PS/2 keyboard data and clock pins.

Syntax

CONFIG ATEMU = int , DATA = data, CLOCK=clock

page -316-