Sei sulla pagina 1di 5

ELEMENTI DI PROGRAMMAZIONE IN LINGUAGGIO P-BASIC

DEFINIZIONI E PRINCIPALI ISTRUZIONI E COMANDI

Definizioni.

6-9VDC 15 14 Vdd 13 12
9 Vdc
Battery Red
Black
X4 X5

Pwr
Vdd Vin Vss
Vss Vss X3
P0 P1
P2 P3 P15
P4 P5 P14
P6 P7 P13
P8 P9 P12
Sout Vin P10 P11 P11
Sin Vss P12 P13 P10
ATN Rst P14 P15 P9
Vss Vdd Vdd Vin P8
P0 P15 X1 P7
P1 P14 P6
P2 P13 P5
P3 P12 P4
P4 P11 P3
P5 P10 P2
Reset P1
P6 P9
P7 P8 P0
X2

0 1 2

BASIC STAMP (BS2). E’ un microcontrollore programmabile in linguaggio P BASIC in grado di


indirizzare 16 bit (0/1) in input ed output. In campo didattico, il BS2 viene utilizzato principalmente
in associazione con una base chiamata BOE. In particolare:
 BOE: è la parte che garantisce un supporto strutturale, è dotata di piste che trasportano i
segnali elettrici da un componente all’altro, dai connettori e dalle prese di alimentazione;
 interfaccia seriale: consente il collegamento della BOE al PC per poter scaricare il
programma;
 bread board: è la piattaforma sulla quale è possibile assemblare dei circuiti sperimentali.
 BS2: è il componente più importante ed ha le capacità di un micro-computer, dotato di
un’unità centrale di calcolo, da una memoria eeprom e un oscillatore (clock) a 40Mhz.

P BASIC. E’ il linguaggio di programmazione della BS2. Il programma dev’essere redatto su


personal computer attraverso un compilatore e successivamente scaricato sul BS2 attraverso un
cavo seriale.

PORTE LOGICHE. Il BS2 è dotato di 24 piedini dei quali 16 sono porte logiche, cioè dei contatti
utilizzabili in input ed output, i rimanenti 8 sono contatti tra cui la messa a terra. Ciascuna porta
logica può essere assimilata ad un interruttore che può essere aperto o chiuso.

LABEL. Con il termine label (etichetta) si identifica una posizione precisa all’interno di un
programma redatto in P BASIC. Lo scopo dell’etichetta è quello di creare un indirizzo rispetto al
quale si possa indicare “un salto di programma”.
1/5
ELEMENTI DI PROGRAMMAZIONE IN LINGUAGGIO P-BASIC
DEFINIZIONI E PRINCIPALI ISTRUZIONI E COMANDI

In informatica esistono due regole molto importanti seguite dai programmatori per contenere gli
errori di programmazione:
- redigere il flow-chart prima della stesura del listato in modo da ordinare logicamente i
passaggi principali del programma;
- redigere il programma un blocco alla volta per passare a quello successivo solo dopo aver
testato con successo solo il blocco precedente.

VARIABILI. Le variabili devono essere dichiarate per prime (dalla seconda riga del listato in poi);
l’utente ha il compito di attribuire un nome alla variabile e la sua tipologia secondo la seguente
sintassi:
X var word dove: X è il nome della variabile, var indica che X è una variabile, word indica il tipo di
variabile (word:alfanumerico; in:input; bin:binario).

COMANDI. Il linguaggio P BASIC è composto da comandi che consentono di gestire le porte


logiche e le variabili, eseguire cicli, sfruttare le label per raggiungere un punto preciso del
programma, effettuare un’azione di contatore per indirizzare il programma davanti ad una scelta
oppure un’azione di monitoraggio delle variabili, etc..

Principali istruzioni e comandi

BRANCHING / PROGRAM CONTROL

GOTO Questo comando (letteralmente vai a) indirizza il puntatore del listato ad una
etichetta specificata consentendo al programma di compiere un salto di istruzioni.

IF...THEN… ELSE It tests a condition and, if that condition is true, goes to a point in the
program specified by an address label. The condition that IF...THEN tests is written as a
mixture of comparison and logic operators.

ON...GOTO. This works exactly the same as the previous IF...THEN example.

BRANCH. E’ di per sé un ciclo “IF THEN” multiplo che consente la gestione di più stati
combinati provenienti da input esterni

LOOPING

DO...LOOP. Loops let your program execute a series of instructions indefinitely, or until a
specified condition terminates the loop.

2/5
ELEMENTI DI PROGRAMMAZIONE IN LINGUAGGIO P-BASIC
DEFINIZIONI E PRINCIPALI ISTRUZIONI E COMANDI

FOR...NEXT Viene utilizzato quando si hanno una o più variabili; la parola FOR viene
seguita dal nome della variabile e subito dopo vengono indicate le specifiche; la parola
NEXT viene inserita nelle righe successive per chiudere il campo di funzionamento

EXIT 2.5

EEPROM ACCESS

RAM ACCESS

NUMERICS
LOOKUP 2

LOOKDOWN

RANDOM. Generates pseudo-random numbers ranging from 0 to 65535.

DIGITAL I/O

INPUT There are several ways to make a pin an input. When the BASIC Stamp is reset, all
of the pins are inputs. Commands that rely on input pins, like PULSIN and SERIN,
automatically change the specified pin to input. Writing 0s to particular bits of the variable
DIRS makes the corresponding pins inputs. And then there’s the INPUT command.
When a pin is an input, your program can check its state by reading from the corresponding
INS variable (PINS on the BS1).

OUTPUT. There are several ways to make a pin an output. Commands that rely on output
pins, like PULSOUT and SEROUT, automatically change the specified pin to output. Writing
1s to particular bits of the variable DIRS makes the corresponding pins outputs. And then
there’s the OUTPUT command.
When a pin is an output, your program can change its state by writing to the corresponding
bit in the OUTS variable (PINS on the BS1).

REVERSE

LOW. The LOW command sets the specified pin to 0 (a 0 volt level) and then sets its mode
to output.

HIGH. The HIGH command sets the specified pin to 1 (a +5 volt level) and then sets its
mode to output.

TOGGLE

PULSIN. PULSIN is like a fast stopwatch that is triggered by a change in state (0 or 1) on the
specified pin.

3/5
ELEMENTI DI PROGRAMMAZIONE IN LINGUAGGIO P-BASIC
DEFINIZIONI E PRINCIPALI ISTRUZIONI E COMANDI

PULSOUT. PULSOUT sets Pin to output mode, inverts the state of that pin; waits for the
specified Duration; then inverts the state of the pin again; returning the bit to its original state.

ASYNCHRONOUS I/0

SERIN. The SERIN command is used to receive asynchronous serial data.

SEROUT. The SEROUT command is used to send asynchronous serial data.

OWOUT

SYNCHRONOUS SERIAL I/O

SHIFTIN

SHIFTOUT

I2CIN

I2COUT

PARALLEL I/O

LCDCMD

LCDIN

LCDOUT

ANALOG I/O

PWM

POT

RCTIME

TIME

PAUSE Questo comando imposta una pausa espressa in millisecondi durante il quale la
BS2 funziona esattamente come un timer.

POLLWAIT

SOUND
4/5
ELEMENTI DI PROGRAMMAZIONE IN LINGUAGGIO P-BASIC
DEFINIZIONI E PRINCIPALI ISTRUZIONI E COMANDI

SOUND. SOUND generates one of 255 square-wave frequencies on an I/O pin. The output
pin should be connected as shown in the circuits below.

FREQOUT. E’ seguito dal numero della porta logica sul quale deve essere attivato, dalla
durata del segnale emesso espressa in millesimi di secondo e dalla frequenza con cui deve
dare il segnale di uscita.

DTMFOUT

POWER CONTROL

NAP

SLEEP

END. END puts the BASIC Stamp into its inactive, low-power mode.

PROGRAM DEBUGGING

DEBUG In informatica la funzione di DEBUG indica un operatore in grado di facilitare


l’individuazione di eventuali anomalie all’interno del programma. Questo comando deve
essere dichiarato nella posizione opportuna durante la stesura del codice e la sua
attivazione, in modalità run, visualizza una finestra nella quale sono riportati i valori della
variabile (contatore, input,…) da monitorare.

5/5

Potrebbero piacerti anche