create switching blinker on the BASCOM - microcontroller

I want to create a switching blinker in BASCOM. I wrote this code for this:
$regfile = "m16def.dat"
$crystal = 1000000
Config Portc = &B11111111
Config Portd.0 = 0
Portd.0 = 1
Portc = &H00
Do
If Portd.0 = 1 Then
Portc = &B11000011
Waitms 500
Portc = &H00
Waitms 500
Else
Portc = &B00111100
Waitms 500
Portc = &H00
Waitms 500
End If
Loop
but the result is this in proteus:
any help would be appreciated.

I read this then I changed If Portd.0 = 1 Then to If Pin = 1 Then and it works. I do not exactly why my first command didn't work!

Related

Octave: How can I plot unknown length serial data using the instrument-control package?

I found this example which sorta works... but it's not perfect. For starters it plots 10 at once but that's an easy fix. Problem is that it does not dynamically get line endings and instead relies on byte count.
pkg load instrument-control
s1 = serialport("/dev/ttyUSB0", 9600)
flush(s1);
y_temp = cell(10,1)
y = 0
while true
for i = 1:10
y_serial = str2num(char(fread(s1,8)))
y_temp {i,1} = y_serial
endfor
y = cat(1, y, y_temp{1:10})
plot(y)
pause(1)
endwhile
srl_close(s1)
This works... so long as the number of bytes per string is 8. How can I make this dynamic and split by line endings?
All I need to do is plot incrementing by 1 x_value and a float y_value from serial.
https://www.edn.com/read-serial-data-directly-into-octave/
Solution found.
Their is no inbuilt solution but you can make your own function like:
function [char_array] = ReadToTermination (srl_handle, term_char)
% parameter term_char is optional, if not specified
% then CR = 'r' = 13dec is the default.
if (nargin == 1)
term_char = 13;
end
not_terminated = true;
i = 1;
int_array = uint8(1);
while not_terminated
val = fread(srl_handle, 1);
if(val == term_char)
not_terminated = false;
end
% Add char received to array
int_array(i) = val;
i = i + 1;
end
% Change int array to a char array and return a string array
char_array = char(int_array);
endfunction
This does not actually work straight off and I had to add \n to my Arduino script.
disp('load instrument control:');
pkg load instrument-control
disp('SerialPort:');
serialportlist
cpx = serialport ("COM6", 57600);
disp(cpx);
disp(sprintf('InBuffer:%3d', cpx.NumBytesAvailable));
if (cpx.NumBytesAvailable > 0)
zx = read(cpx, cpx.NumBytesAvailable);
disp(sprintf("IN:\r\n%s", zx));
endif;

Arduino Pushbutton Controlling VB6 shape fill color

I'm creating a program where if I click push button in Arduino the shape fill color in VB6 will change to red and if I click the pushbutton again the fill color will change to green. I am having problems reading serial data sent by my Arduino to VB6.
Here's my Arduino code:
int pbuttonPin = 7;// push button
int LED = 8; // LED
int val = 0; // push value from pin 2
int lightON = 0;//light status
int pushed = 0;//push status
void setup()
{
Serial.begin(9600);
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
}
void loop()
{
val = digitalRead(pbuttonPin);// read the push button value
if(val == HIGH && lightON == LOW){
pushed = 1-pushed;
delay(100);
}
lightON = val;
if(pushed == HIGH)
{
Serial.print("Color Red\n");
digitalWrite(LED, LOW);
delay(100);
}
else
{
Serial.print("Color Green\n");
digitalWrite(LED, HIGH);
delay(100);
}
}
Here's my VB6 code,
Private Sub Form_Load()
With MSComm1
.CommPort = 8
.Settings = "9600,N,8,1"
.Handshaking = comRTS
.RTSEnable = True
.DTREnable = True
.RThreshold = 1
.SThreshold = 1
.InputMode = comInputModeText
.InputLen = 0
.PortOpen = True
End With
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
If MSComm1.Input = "Color Red" Then
Shape1.FillColor = vbRed
Shape1.FillStyle = vbSolid
End If
If MSComm1.Input = "Color Green" Then
Shape1.FillColor = vbGreen
Shape1.FillStyle = vbSolid
End If
End Sub
Thank you very much for offering your assistance on the upcoming
There is no guarantee the data from the serial port will not be broken down into multiple parts so you should implement a buffer to keep track of all the parts received. Also, since MSComm1.Input returns and removes characters from the receive buffer, your second If statement will never contain any data. You should read the data once and store it in a variable. Here's some code that implements this:
Dim m_sBuffer As String
Private Sub Form_Load()
' Initialize Serial Port
With MSComm1
.CommPort = 8
.Settings = "9600,N,8,1"
.Handshaking = comRTS
.RTSEnable = True
.DTREnable = True
.RThreshold = 1
.SThreshold = 1
.InputMode = comInputModeText
.InputLen = 0
.PortOpen = True
End With
' Initialize FillStyle
Shape1.FillStyle = vbSolid
' Clear buffer
m_sBuffer = ""
' Start timer
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim sReceivedData As String
' Read data from serial port
sReceivedData = MSComm1.Input
' Append Received Data to buffer
m_sBuffer = m_sBuffer & sReceivedData
' Check buffer content
Select Case m_sBuffer
Case "Color Red"
Shape1.FillColor = vbRed
m_sBuffer = "" ' Clear buffer
Case "Color Green"
Shape1.FillColor = vbGreen
m_sBuffer = "" ' Clear buffer
End If
End Sub

how can i activate UART(rs-232) with register setting at PICC?

I know there is #use rs232 command to activate it,
but i want to know how to set registers individually to activate rs232
i'm using PIC18f13k22.
I did like this:
INTCON = 0xc2;
IPR1 = 0x7f;
PIE1 = 0x20;
PIR1 = 0x10;
BAUDCON = 0x48;
RCSTA = 0x90;
TXSTA = 0xA6;
SPBRG = 0x82;
SPBRGH = 0x06;
I looked up PIC18f13k22 datasheet, and found related registers, and set like that.
and didn't work.
need help!
thanks
Here is a complete code that builds with Microchip MPLABX and XC8 v2.30:
/*
* File: main.c
* Author: dan1138
* Compiler: XC8 v2.30
*
* Created on October 6, 2020, 1:41 PM
*/
#pragma config FOSC = IRC, PLLEN = OFF, PCLKEN = ON, FCMEN = OFF, IESO = OFF
#pragma config PWRTEN = OFF, BOREN = SBORDIS, BORV = 19, WDTEN = OFF
#pragma config WDTPS = 32768, HFOFST = ON, MCLRE = ON, STVREN = ON, LVP = OFF
#pragma config BBSIZ = OFF, XINST = OFF, DEBUG = OFF, CP0 = OFF, CP1 = OFF
#pragma config CPB = OFF, CPD = OFF, WRT0 = OFF, WRT1 = OFF, WRTC = OFF
#pragma config WRTB = OFF, WRTD = OFF, EBTR0 = OFF, EBTR1 = OFF, EBTRB = OFF
#include <xc.h>
#include <stdio.h>
void putch(char txData)
{
if(1 == RCSTAbits.OERR)
{
RCSTAbits.CREN = 0;
RCSTAbits.CREN = 1;
}
while(0 == PIR1bits.TXIF)
{
}
TXREG = txData;
}
void main(void)
{
OSCCON = 0x60;
OSCCON2 = 0x04;
OSCTUNE = 0x00;
LATA = 0x00;
LATB = 0x00;
LATC = 0x00;
TRISA = 0x37;
TRISB = 0x70;
TRISC = 0xFF;
ANSEL = 0xFF;
ANSELH = 0x07;
WPUB = 0x00;
WPUA = 0x00;
INTCON2bits.nRBPU = 1;
BAUDCON = 0x08;
RCSTA = 0x90;
TXSTA = 0x24;
SPBRG = 0xCF;
SPBRGH = 0x00;
PIR1bits.TXIF = 0;
printf("\r\nPIC18F13K22 UART demo\r\n");
while (1)
{
}
}
The solution to your problem lies in knowing what to set the configuration word values to, and how to comprehend what information can be found in the data sheet.
I know the data sheet is dense and only documents what can be selected, but not why specific values should be chosen.
With more experience you can learn how to read data sheets and what can be useful.
As you made no effort to comment in the code you posted my code is likewise missing anything close to a useful comment.

STM32F4 DMA doesn't control ADC channels

I'm trying to set up my STM32F407-Discovery board to read multiple ADC channels from ADC1 using the DMA controller. I can easily read the analog value one at a time without using DMA, but as soon as I enable DMA for the ADC, ADC1->DR is always 0, and adc_vals is filled with zeros. Furthermore, it hangs on while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));.
EDIT: it would appear DMA_GetCmdStatus is returning DISABLED. Any ideas?
Is there a way to start the ADC or something that I'm missing?
//setup adc1: in1,2,3,8,9,15
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); //adc1 on the apb2 peripheral bus
ADC_InitTypeDef adc;
ADC_DeInit(); //set adc to default state
adc.ADC_DataAlign = ADC_DataAlign_Right;
adc.ADC_Resolution = ADC_Resolution_12b;//12 bit = 4096
adc.ADC_ContinuousConvMode = ENABLE;
adc.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
adc.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
adc.ADC_NbrOfConversion = NUM_ADC;
adc.ADC_ScanConvMode = ENABLE;
ADC_Init(ADC1,&adc);
ADC_DMACmd(ADC1, ENABLE); //enable adc for dma. When this line is removed, I see data on ADC1->DR
ADC_Cmd(ADC1,ENABLE);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_144Cycles);//1:1710, 0
//ADC_RegularChannelConfig(ADC1,ADC_Channel_2,2,ADC_SampleTime_144Cycles);//2:1710, 0
//ADC_RegularChannelConfig(ADC1,ADC_Channel_3,3,ADC_SampleTime_144Cycles);//3:1710, 0
ADC_RegularChannelConfig(ADC1,ADC_Channel_8,4,ADC_SampleTime_144Cycles);//8:3520
ADC_RegularChannelConfig(ADC1,ADC_Channel_9,5,ADC_SampleTime_144Cycles);//9:1000
//ADC_RegularChannelConfig(ADC1,ADC_Channel_15,6,ADC_SampleTime_144Cycles);//15:3920
//DMA for multiple adc channels:
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); //dma1 clock enable
DMA_InitTypeDef dma;
DMA_DeInit(DMA2_Stream0); //reset DMA2 stream 0 to default values
dma.DMA_Channel = DMA_Channel_0;
dma.DMA_PeripheralBaseAddr = (uint32_t)&(ADC1->DR);
dma.DMA_Memory0BaseAddr = (uint32_t)&adc_vals[0];
dma.DMA_DIR = DMA_DIR_PeripheralToMemory;
dma.DMA_BufferSize = NUM_ADC;
dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
dma.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
dma.DMA_Mode = DMA_Mode_Circular;
dma.DMA_Priority = DMA_Priority_High;
dma.DMA_FIFOMode = DMA_FIFOMode_Disable;
dma.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
dma.DMA_MemoryBurst = DMA_MemoryBurst_Single;
dma.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &dma);
DMA_ITConfig(DMA2_Stream0, DMA_IT_TC | DMA_IT_HT, ENABLE); //Enable DMA Stream Half / Transfer Complete interrupt
DMA_Cmd(DMA2_Stream0, ENABLE); //DMA2_Stream0 enable
//dma transfer complete interrupt:
NVIC_InitTypeDef nvic;
//Enable DMA1 channel IRQ Channel
nvic.NVIC_IRQChannel = DMA2_Stream0_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 0;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvic);
ADC_SoftwareStartConv(ADC1);//Start the adc conversion
At least you can try:
change DMA_PeripheralInc to enable
use ADC_DMARequestAfterLastTransferCmd(ADC, Enable); for dma request after group conversion.
init ADC_CommonInitTypeDef
init GPIO, if not
Moreover enabling ADC before all the configurations is strange.
In my opinion the best way is to copy-paste "ADC_DualModeRegulSimu"example from standard peripherals library. It contains beautiful code exactly for your purpose.
There were a number of errors. Here's my working code for anybody else having the same trouble:
//DMA for multiple adc channels:
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ADC1 on APB2 peripheral bus
DMA_InitTypeDef dma; //dma2/stream0/channel0
dma.DMA_Channel = DMA_Channel_0;
dma.DMA_Memory0BaseAddr = (uint32_t)&adcVal;
dma.DMA_PeripheralBaseAddr = (uint32_t)&(ADC1->DR);
dma.DMA_DIR = DMA_DIR_PeripheralToMemory;
dma.DMA_BufferSize = 6;
dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
dma.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
dma.DMA_Mode = DMA_Mode_Circular;
dma.DMA_Priority = DMA_Priority_High;
dma.DMA_FIFOMode = DMA_FIFOMode_Enable;
dma.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
dma.DMA_MemoryBurst = DMA_MemoryBurst_Single;
dma.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &dma);
DMA_Cmd(DMA2_Stream0, ENABLE); //dMA2_Stream0 enable
//analog inputs: PA1,2,3, PC5, PB0,1
gpio.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
gpio.GPIO_Mode = GPIO_Mode_AN;
gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &gpio);
gpio.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOC, &gpio);
gpio.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOB, &gpio);
//ADC common init
ADC_CommonInitTypeDef adcCommon;
adcCommon.ADC_Mode = ADC_Mode_Independent;
adcCommon.ADC_Prescaler = ADC_Prescaler_Div2;
adcCommon.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
adcCommon.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&adcCommon);
//setup adc1: in1,2,3,8,9,15
ADC_InitTypeDef adc;
ADC_DeInit(); //set adc to default state
adc.ADC_DataAlign = ADC_DataAlign_Right; //mask 0b 00001111 11111111
adc.ADC_Resolution = ADC_Resolution_12b;//12 bit = 4096
adc.ADC_ContinuousConvMode = ENABLE; //continuous: constantly converting data - can always read register
adc.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;//external trigger conversion (?)
adc.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
adc.ADC_NbrOfConversion = 6;
adc.ADC_ScanConvMode = ENABLE;//single/multichannel
ADC_Init(ADC1,&adc);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_56Cycles); ADC_RegularChannelConfig(ADC1,ADC_Channel_2,2,ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_3,3,ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_8,4,ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_9,5,ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_15,6,ADC_SampleTime_56Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); //single adc repeated
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1,ENABLE);
Thanks for the code ethan, made minor mod to fit my case and to work as a general function.
volatile uint16_t adcVal[6];
void Setup_DMA_ADC( void )
{
GPIO_InitTypeDef gpio;
//DMA for multiple adc channels:
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ADC1 on APB2 peripheral bus
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
DMA_InitTypeDef dma; //dma2/stream0/channel0
dma.DMA_Channel = DMA_Channel_0;
dma.DMA_Memory0BaseAddr = (uint32_t) &adcVal[0];
dma.DMA_PeripheralBaseAddr = (uint32_t)&(ADC1->DR);
dma.DMA_DIR = DMA_DIR_PeripheralToMemory;
dma.DMA_BufferSize = 6;
dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
dma.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
dma.DMA_Mode = DMA_Mode_Circular;
dma.DMA_Priority = DMA_Priority_High;
dma.DMA_FIFOMode = DMA_FIFOMode_Enable;
dma.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
dma.DMA_MemoryBurst = DMA_MemoryBurst_Single;
dma.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &dma);
DMA_Cmd(DMA2_Stream0, ENABLE); //dMA2_Stream0 enable
//analog inputs: PA1,2,3, PC5, PB0,1
GPIO_StructInit(&gpio);
gpio.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
gpio.GPIO_Mode = GPIO_Mode_AN;
gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &gpio);
gpio.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOC, &gpio);
gpio.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOB, &gpio);
//ADC common init
ADC_CommonInitTypeDef adcCommon;
adcCommon.ADC_Mode = ADC_Mode_Independent;
adcCommon.ADC_Prescaler = ADC_Prescaler_Div2;
adcCommon.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
adcCommon.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&adcCommon);
//setup adc1: in1,2,3,8,9,15
ADC_InitTypeDef adc;
ADC_DeInit(); //set adc to default state
adc.ADC_DataAlign = ADC_DataAlign_Right; //mask 0b 00001111 11111111
adc.ADC_Resolution = ADC_Resolution_12b;//12 bit = 4096
adc.ADC_ContinuousConvMode = ENABLE; //continuous: constantly converting data - can always read register
adc.ADC_ExternalTrigConv = DISABLE;//ADC_ExternalTrigConv_T1_CC1;//external trigger conversion (?)
adc.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
adc.ADC_NbrOfConversion = 6;
adc.ADC_ScanConvMode = ENABLE;//single/multichannel
ADC_Init(ADC1,&adc);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_2,2,ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_3,3,ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_8,4,ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_9,5,ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_15,6,ADC_SampleTime_56Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); //single adc repeated
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1,ENABLE);
// Start ADC conversion
ADC_SoftwareStartConv(ADC1);
return;
}

Multi dimensional array assignment in verilog, without loop?

How can one assign a multi dimensional array if it is a wire, in a single line?
assign TO[W1:0][W2:0] = cntrl ? FROM1[W1:0][W2:0] : FROM2[W1:0][W2:0];
I get a syntax error if I use this.
Is there any other way other than using generate or for loops?
You need to be using SystemVerilog to make aggregate assignments to arrays.
To assign an unpacked arrays braces with tick '{ and } are used, provided all the values of the array should be assigned.
usage example
module top ( input i);
wire d [0:1][0:3];
wire a [0:1][0:3]='{ '{1,1,1,1}, '{1,1,1,1} };
wire b [0:1][0:3]='{ '{0,0,0,0}, '{0,0,0,0} };
assign d = i? (' { '{a[0][0],a[0][1],a[0][2],a[0][3]},'{b[1][0],b[1][1],b[1][2],b[1][3]}}):
(' { '{b[1][0],b[1][1],b[1][2],b[1][3]},'{a[0][0],a[0][1],a[0][2],a[0][3]}});
endmodule
Here wire a [0:1][0:3]='{ '{1,1,1,1}, '{1,1,1,1} }; and wire b [0:1][0:3]='{ '{0,0,0,0}, '{0,0,0,0} }; represents
// a[0][0] = 1 b[0][0] = 0
// a[0][1] = 1 b[0][1] = 0
// a[0][2] = 1 b[0][2] = 0
// a[0][3] = 1 b[0][3] = 0
// a[1][0] = 1 b[1][0] = 0
// a[1][1] = 1 b[1][1] = 0
// a[1][2] = 1 b[1][2] = 0
// a[1][3] = 1 b[1][3] = 0
Working example can be found in the eda-playground link
Two pure verilog solutions: (Assuming W1 and W2 are parameters, not variables)
// 'TO' must be a reg
integer i,j;
always #* begin
for(i=0; i<=W1; i=i+1) begin
for(j=0; j<=W2; j=j+1) begin
TO[i][j] = cntrl ? FROM1[i][j] : FROM2[i][j];
end
end
end
// 'TO' must be a wire
genvar i,j;
generate
for(i=0; i<=W1; i=i+1) begin
for(j=0; j<=W2; j=j+1) begin
assign TO[i][j] = cntrl ? FROM1[i][j] : FROM2[i][j];
end
end
endgenerate

Resources